如何让 jQuery Validate 触发“成功”方法“onchange”而不是“onblur”对于选择菜单?
我正在使用 jQuery 验证插件。我正在使用 show()
方法在页面上没有错误后显示提交按钮。
我有一个只有一个选择/下拉菜单的表单。我遇到的问题是,当我在选择中选择一个选项时,它不会显示提交按钮,直到我单击页面上的其他位置,而当我选择有效选项时它应该立即显示它。
有谁知道如何让它触发 success()
onchange
或 onkeyup
而不是 onblur
?
这是我正在做的一个基本示例:
$form.validate( {
rules: {
favoriteSport: {
required: true
}
},
messages: {
favoriteSport: "This is required"
},
success: function() {
console.log("yay, it was valid!");
$("#submit").show();
}
});
I'm using the jQuery validate plug-in. I am using the show()
method to display the submit button once there are no errors on the page.
I have a form that just has a single select/drop-down menu. The issue I am running into is when I select an option in the select, it doesn't show the submit button until I click somewhere else on the page, when it should show it as soon as I select a valid option.
Does anyone know how to get it to fire the success()
onchange
or onkeyup
instead of onblur
?
Here's a basic example of what I'm doing:
$form.validate( {
rules: {
favoriteSport: {
required: true
}
},
messages: {
favoriteSport: "This is required"
},
success: function() {
console.log("yay, it was valid!");
$("#submit").show();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终添加了对 bind() 的单独调用,因此每当发生更改事件时,它都会针对选择菜单的 ID 运行 $validate() 。
很难相信它不是内置在插件中的,因为它是最流行的 jQuery 表单验证插件。
这是一个例子:
I ended up adding a separate call to bind() so whenever a change event occurs, it runs $validate() agains the ID of the select menu.
It's hard to believe this isn't built in to the plug-in seeing how it's the most popular form validation plug-in for jQuery.
Here's an example:
尝试这个
.change()
而不是.blur()
....try this
.change()
instead of.blur()
....自从提出这个问题以来已经很长时间了,我仍然不相信插件开发人员已经提供了针对此问题的本机修复。 @Cofey,你走在正确的轨道上,我认为对你的答案逻辑的轻微改进将有助于使所有选择的解决方案动态化:
It's been a long time since this question was asked and I still don't believe a native fix to this issue has been provided by the plugin developer. @Cofey you were on the right track and I think a slight improvement to your answer's logic would help to make the solution dynamic for all selects: