jquery中模糊效果触发后的回调函数

发布于 2024-09-14 18:52:15 字数 676 浏览 4 评论 0原文

我有一个表单,一旦用户“模糊”出输入字段,该表单就会被验证。所以:

$("#field1").blur(function() {

if($(this).val().length>0) {
   $(this).attr('class', 'completed');
}

});

当用户第一次访问表单时,直到所有字段都“完成”时,提交按钮是隐藏的:

$(".submit").hide();

我希望提交按钮在所有 #field1、#field2 ... 元素都被分配了“完成”后向下滑动“类(建议的任何其他方法也可以)。

我尝试使用:

$("#field1").blur(function() {
    if($(".completed").length == $("input[type='text']").length) {
        $(".submit").slideDown(1000);
    }
});

但它不起作用,因为它是与之前的“模糊”处理程序同时调用的。

是否有一种方法可以在另一个事件处理程序触发后将其绑定到?也许有一种方法可以将回调函数添加到“模糊”中?

我很感激所有类型的解决方案(不一定是我提出的那样)。

I have a form that is validated once the user "blur"s out of an input field. So:

$("#field1").blur(function() {

if($(this).val().length>0) {
   $(this).attr('class', 'completed');
}

});

When the user first visits the form, and up until all fields are "completed", the submit button is hidden:

$(".submit").hide();

I want the submit button to slideDown once all #field1, #field2 ... elements have been assigned a "completed" class (any other method suggested will work as well).

I tried using:

$("#field1").blur(function() {
    if($(".completed").length == $("input[type='text']").length) {
        $(".submit").slideDown(1000);
    }
});

But it didn't work as it was called concurrently with the previous "blur" handler.

Is there a method to bind an event handler to after another has triggered? Maybe a way to add a callback function to "blur" that works?

I'd appreciate all types of solution (not necessarily somethin like I presented).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‘画卷フ 2024-09-21 18:52:15

好吧,您不一定需要设置 css 类来执行此类验证:

$("#field1").blur(function() {
    if($("input[type='text']"),filter(isCompleted).length == $("input[type='text']").length) {
        $(".submit").slideDown(1000);
    }
});

function isCompleted(i) {
    return $(this).val().length > 0;
}

另一种方法可能是:

$("#field1").blur(function() {
    // If there are not empty textboxes, slidedown...
    if($(":text[value='']").length === 0) {
        $(".submit").slideDown(1000);
    }
});

Well, you don't necessarily need to set css classes for doing these kind of validations:

$("#field1").blur(function() {
    if($("input[type='text']"),filter(isCompleted).length == $("input[type='text']").length) {
        $(".submit").slideDown(1000);
    }
});

function isCompleted(i) {
    return $(this).val().length > 0;
}

Another way could be:

$("#field1").blur(function() {
    // If there are not empty textboxes, slidedown...
    if($(":text[value='']").length === 0) {
        $(".submit").slideDown(1000);
    }
});
葮薆情 2024-09-21 18:52:15

只需在第一个 .blur 方法中进行检查,尽管我建议使用类名来防止页面上的任何其他输入干扰。

$(".input-field").blur(function() {
  if($(this).val().length>0) {
   $(this).addClass('completed');
  }
  setTimeout("isFormValid()", 100);
});

function isFormValid() {
  if($(".completed").length == $(".input-field").length) {
    $(".submit").slideDown(1000);
  }
}

Just do your check inside of your first .blur method, though I would suggest using a class name to prevent any other inputs on the page interfering.

$(".input-field").blur(function() {
  if($(this).val().length>0) {
   $(this).addClass('completed');
  }
  setTimeout("isFormValid()", 100);
});

function isFormValid() {
  if($(".completed").length == $(".input-field").length) {
    $(".submit").slideDown(1000);
  }
}
偷得浮生 2024-09-21 18:52:15

怎么样:

$('input').blur(function() {
  if (this.value.length > 0)
    $(this).addClass('validationError');
  else
    $(this).removeClass('validationError');

  validateForm($(this).parents('form'));
});

funciton validateForm($form) {
  if ($form.has('.error').length > 0)
    $('.submit').slideUp();
  else
    $('.submit').slideDown();
}

How about something like:

$('input').blur(function() {
  if (this.value.length > 0)
    $(this).addClass('validationError');
  else
    $(this).removeClass('validationError');

  validateForm($(this).parents('form'));
});

funciton validateForm($form) {
  if ($form.has('.error').length > 0)
    $('.submit').slideUp();
  else
    $('.submit').slideDown();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文