jQuery模糊备选方案

jQuery blur alternative

本文关键字:方案 模糊 jQuery      更新时间:2023-09-26

可能重复:
检测对<input type="text">(立即(使用JQuery

我有一个文本框,用户可以在其中输入数据,在onBlur事件中,它会发出AJAX请求来验证他们输入的数据。如果它有效,则按钮变为启用状态。然而,用户感到困惑的是,他们必须签出或单击页面上的某个位置才能启用该按钮。

对于我正在使用的代码,是否有其他事件或方法?

$('.serial').live('blur', function () {
  //Do AJAX to validate
  //If ok enable button
  $('#button').attr("disabled", "");
});

您可以使用keyup,也许可以添加一个setTimeout来延迟请求。

尝试keyup-按下每个键后都会触发。

$('.serial').live('keyup', function () {
    //Do AJAX to validate
    //If ok enable button
    $('#button').attr("disabled", "");
});

每次按键发送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 

在验证时禁用文本框也可能有所帮助。以便用户能够理解正在进行一些处理(此处为验证(。并且现在需要等待。

我决定使用T.J Crowder发布的链接中的答案。

 $(document).ready(function () {
    MonitorSerialTextBoxes();
    $('#newSerial').click(function () {
       $.tmpl("productTemplate", mymodel).insertAfter($(".entry").last());
       MonitorSerialTextBoxes();
    });
});