未定义 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
jQuery onkeyup method not defined
提问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
回答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});
});