所以我有一個功能,當用戶在輸入字段中輸入5個字符時觸發。輸入5個字符時執行的按鍵事件
但是,每當我在5完成後放置其他角色時,它都會繼續發射事件。
如何防止發生這種情況?
$(function(){
var $zipEntry = $('#zipEntry').bind('keyup',function(event) { //bind to key up, doesn't require
//make sure that there are 5 numbers... or length = 5 since numeric only input
var $this = $(this); // cache $(this)
if ($this.val().length == 5){ // use cached
console.log("There are 5 characters!!");
$this.trigger('zipEntered');
}
});
每次你在一個keyup上調用匿名函數時,你正在重置var $ this = $(this) – Prescott 2010-11-11 22:31:37
我沒有看到任何問題。你可以在jsbin.com上做一個演示嗎? – lincolnk 2010-11-11 22:37:10
只有當它等於5時不小於5或greathan 5 – kobe 2010-11-11 22:38:40