问题描述
我在以下位置使用了jquery验证插件:
I am using a jquery validation plugin at:
http://bassistance.de/jquery-plugins/jquery-plugin-validation /
我只想在验证通过后禁用提交按钮:
I want to disabled submit button ONLY when validation passes:
它尝试了
$("#submit").click(function()
{
$(this).attr("disabled", "disabled");
});
但是,即使验证失败,也会禁用提交"按钮.是否只有在验证通过并发生Submit事件时才禁用提交的方法?
But this disables the submit button even if validation fails. Is there a way to only disable the submit when validation passes and a submit event occurs?
推荐答案
浏览源代码,它看起来就像您应该能够覆盖invalidHandler
一样,类似于以下内容:
Looking through the source, it looks like you should be able to override the invalidHandler
, something along the lines of:
$("#form").validate({
submitHandler: function(form) {
// disable your button here
form.submit();
},
invalidHandler: function() {
// re-enable the button here as validation has failed
}
});
乍一看,这似乎是您想使用validate
插件进行此操作的方式,但是很遗憾,我没有时间atm来摆弄它以确保 sure 可以.
At a glance, this seems to be the way you'd want to go about doing this with the validate
plugin, but I unfortunately don't have time atm to fiddle it to make sure it works.
这应该做(我使用一个名称为fname
的单个文本字段进行测试):
This should do 'er (I used a single text field with the name fname
for testing):
$('#frm').bind('invalid-form.validate', function(){
// you can add extra validation handling here if you want to
});
$('#frm').validate({
rules: {
fname: "required"
},
submitHandler: function(form){
$('form input[type=submit]').attr('disabled', 'disabled');
form.submit();
}
});
验证通过后,
submitHandler
仅被 击中.
submitHandler
is only hit once validation has passed.
这篇关于jQuery Validate插件防止双重提交验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!