未定义 jQuery onkeyup 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11086048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:10:57  来源:igfitidea点击:

jQuery onkeyup method not defined

jquerykey-events

提问by Julian

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:

应该没有定义 onkeyup 方法,但是,我的 ide 自动向我推荐了该方法。当我在 chrome 开发工具中查看错误时,我收到错误 Uncaught TypeError: Object [object Object] has no method '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>

回答by thecodeparadox

$("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

jQuery 没有方法名 onkeyup

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

阅读更多关于.keyup().on()

回答by cloakedninjas

The method is:

方法是:

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

回答by cloakedninjas

instead of onkeyup use keyup

而不是 onkeyup 使用 keyup

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

回答by ShankarSangoli

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

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

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