سؤال

the onkeyup method is supposedly not defined, however, the method is auto-recommended to me by my ide. When I view the error in chrome dev tools I get the error Uncaught TypeError: Object [object Object] has no method 'onkeyup'. I am using the latest version of jQuery. Here is my code:

    <!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function(){
                var str = document.getElementById('txt1').value;
                $.post("addName.php", {q: str});
            });
            $("input").onkeyup(function(){
                var str = document.getElementById('txt1').value;
                $.post("gethint.php", {q: str});
            });
        });
    </script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action="">
    First name: <input type="text" id="txt1"/>
</form>
<p>Suggestions: <span id="txtHint"></span></p>
<button> Add Name</button>

</body>
</html>
هل كانت مفيدة؟

المحلول

$("input").on('keyup', function(){
    var str = document.getElementById('txt1').value;
    $.post("gethint.php", {q: str});
});

or

$("input").keyup( function(){
    var str = document.getElementById('txt1').value;
    $.post("gethint.php", {q: str});
});

jQuery has no method name onkeyup

Read more about .keyup() and .on()

نصائح أخرى

The method is:

$("input").keyup(function() {
})

instead of onkeyup use keyup

 $("input").keyup(function(){
                var str = document.getElementById('txt1').value;
                $.post("gethint.php", {q: str});
            });

The method onkeyup doesn't exist, the error message clearly says "onkeyup method not defined". It should be just keyup.

$("input").keyup(function() {
    //Your code
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top