2011-12-02 51 views
3

可能重複:
Detect all changes to a <input type=「text」> (immediately) using JQueryjQuery的模糊替代

我有一個用戶輸入數據和在onBlur事件它AJAX請求來驗證他們已經輸入的數據的文本框。如果驗證,則按鈕變爲啓用。然而,用戶會感到困惑,他們必須在頁面上的某個地方標出或點擊該按鈕才能啓用。

是否有替代事件或方法來使用我正在使用的代碼?

$('.serial').live('blur', function() { 

    //Do AJAX to validate 

    //If ok enable button 
    $('#button').attr("disabled", ""); 

}); 
+2

所以很多重複,這麼短的時間... ;-)這可能是最好的:http://stackoverflow.com/questions/1948332/detect-all-changes-to-a-input-type -text-immediately-using-jquery但也有:http://stackoverflow.com/questions/1539279/using-jquery-how-do-you-detect-if-the-value-of-a-text-input -has-changed-while-t | http://stackoverflow.com/questions/1481152/jquery-how-to-detect-a-textboxs-content-has-changed | http://stackoverflow.com/questions/6153047/jquery-detect-changed-input-text-box –

回答

0

我決定使用鏈接T.JCrowder發佈的鏈接this答案。

$(document).ready(function() { 

    MonitorSerialTextBoxes(); 

    $('#newSerial').click(function() { 

     $.tmpl("productTemplate", mymodel).insertAfter($(".entry").last()); 
     MonitorSerialTextBoxes(); 

    }); 

}); 
1

嘗試keyup - 按下每個按鍵後它會被觸發。

$('.serial').live('keyup', function() { 
    //Do AJAX to validate 

    //If ok enable button 
    $('#button').attr("disabled", ""); 
}); 
+0

但不是如果他們將數據粘貼到字段 – Alnitak

+0

是的,它確實:http://jsfiddle.net/RoryMcCrossan/A9WK6/ –

+0

KeyUp是有道理的,它只會讓很多AJAX請求 – Jon

4
+0

你能爲setTimeout做一些特定的元素嗎?例如$('。serial') – Jon

+0

@Jon:當然,請參閱我提供的第二個鏈接中接受的答案。 'delay'函數只針對你附加keyup事件的元素 – nico

1

發送每個按鍵的AJAX請求將會產生巨大的請求隊列。如果串行有字符的固定數量或它有一個固定的模式,然後

var validate = function(value){ 
    //Check for number of characters or do regex validate the value 
    //If Validated send ajax request otherwise not 
} 

$('.serial').live('keyup', function(){ 
    validate(value); 
}); 

$('.serial').live('blur', function() { 
    validate(value); 
}); 

//tracking the mouseover on the submit button may also help 
//as its common psychology of the users to play with the submit button 
//after the entered the data and nothing is happening 

也禁用文本框在驗證也可以幫助你可以限制這一點。使用戶可以理解某些處理(在此驗證)正在進行。和現在需要等待。