jQuery keyup方法没有定义

时间:2012-06-18 15:31:07

标签: jquery key-events

onclup方法据说没有定义,但是我的ide会自动推荐这个方法。当我在chrome dev工具中查看错误时,我得到错误Uncaught TypeError:Object [object Object]没有方法'onkeyup'。我正在使用最新版本的jQuery。这是我的代码:

    <!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>

4 个答案:

答案 0 :(得分:16)

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

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

jQuery没有方法名称onkeyup

详细了解 .keyup() .on()

答案 1 :(得分:1)

方法是:

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

答案 2 :(得分:0)

而不是onkeyup使用keyup

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

答案 3 :(得分:0)

方法onkeyup不存在,错误消息明确指出"onkeyup method not defined"。它应该只是keyup

$("input").keyup(function() {
    //Your code
});