2011-12-02 77 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 

也禁用文本框在验证也可以帮助你可以限制这一点。使用户可以理解某些处理(在此验证)正在进行。和现在需要等待。