一个更优雅且快速的解决方案是简单地替代默认的点击和上KEYUP事件添加代码,你需要如下的而不是添加另一个听的事件
$("#form").validate({
submitHandler: function(form) {
form.submit();
},
onkeyup: function(element, event) {
if (event.which === 9 && this.elementValue(element) === "") {
return;
} else if (element.name in this.submitted || element === this.lastElement) {
this.element(element);
}
if (this.checkForm()) { // checks form for validity
$('a.submit').attr('class', 'submit btn btn-success'); // enables button
} else {
$('a.submit').attr('class', 'submit btn btn-danger disabled'); // disables button
}
},
onclick: function(element) {
// click on selects, radiobuttons and checkboxes
if (element.name in this.submitted) {
this.element(element);
// or option elements, check parent select in that case
} else if (element.parentNode.name in this.submitted) {
this.element(element.parentNode);
}
if (this.checkForm()) { // checks form for validity
$('a.submit').attr('class', 'submit btn btn-success'); // enables button
} else {
$('a.submit').attr('class', 'submit btn btn-danger disabled'); // disables button
}
}
})
随着下面的CSS代码提交触发 - 最初禁用(引导3班):
<a onclick="if(!$(this).hasClass('disabled')) { $('#form').submit(); }" class="submit btn btn-danger disabled">button_save</a>
这可以很容易地与jQuery插件areYouSure使用,只启用提交时实际更改:
if (this.checkForm() && $('#form').hasClass('dirty')) { // checks form for validity
初始化插件使用无声:
$('#form').areYouSure({'silent':true});
菲尔,你到底来解决这一问题?你能提供你的最终解决方案吗? – Blair 2010-12-16 04:15:37
@blairyeah - 查看Michael Irgoyen的答案 – ripper234 2012-03-07 12:20:21