admin管理员组文章数量:1426943
There's an input text fires a function on "onkeyup" event. When that function fires I'm binding a "keyup" event with .on("keyup") method to get the last pressed key to find out if it was alphanumerical in order to fire the search ajax I want. And after I finish, I unbind the connection with .off("keyup") method.
But there's a problem with the unbindation. The problem is; My code runs once and doesn't run for a turn, then runs again and doesn't run for the other turn and keeps it going like that. I replaced the code I want to simplify it to test.
$("#arama").on("keyup",function(event) {
console.log("asd");
$("#arama").off("keyup");
});
<script src=".1.1/jquery.min.js"></script>
<input id="arama" />
There's an input text fires a function on "onkeyup" event. When that function fires I'm binding a "keyup" event with .on("keyup") method to get the last pressed key to find out if it was alphanumerical in order to fire the search ajax I want. And after I finish, I unbind the connection with .off("keyup") method.
But there's a problem with the unbindation. The problem is; My code runs once and doesn't run for a turn, then runs again and doesn't run for the other turn and keeps it going like that. I replaced the code I want to simplify it to test.
$("#arama").on("keyup",function(event) {
console.log("asd");
$("#arama").off("keyup");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="arama" />
Where am I mistaken ?
P.S: I've solved it thanks to Chris.
Share Improve this question edited Apr 15, 2017 at 15:53 Ümit Aparı asked Apr 15, 2017 at 15:02 Ümit AparıÜmit Aparı 5682 gold badges7 silver badges24 bronze badges2 Answers
Reset to default 1Your code works for me (see below). Maybe check where you are binding your keyup
event. It should be bound once when the document loads before the page shows. If you bind it multiple times (i.e. if the code that contains your keyup
function runs more than once) you will run into problems.
$("#arama").on("keyup", function(event) {
var i = event.keyCode;
if ((i >= 48 && i <= 57) || (i >= 96 && i <= 105)) {
$("#arama").off("keyup");
console.log("Number pressed. Stopping...");
} else {
console.log("Non-number pressed.");
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="arama" />
No need to unbind the event. Try this
$("#arama").on("keyup",function(event) {
console.log("asd");
});
本文标签: jqueryJavaScript on(quotkeyupquot)Stack Overflow
版权声明:本文标题:jquery - JavaScript .on("keyup") - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470666a2659733.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论