jquery中模糊效果触发后的回调函数
我有一个表单,一旦用户“模糊”出输入字段,该表单就会被验证。所以:
$("#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您不一定需要设置 css 类来执行此类验证:
另一种方法可能是:
Well, you don't necessarily need to set css classes for doing these kind of validations:
Another way could be:
只需在第一个 .blur 方法中进行检查,尽管我建议使用类名来防止页面上的任何其他输入干扰。
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.
怎么样:
How about something like: