使用 jquery 禁用/启用整个表单的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/630535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:29:14  来源:igfitidea点击:

Disable/enable entire form's submit with jquery

jqueryvalidation

提问by Boris Callens

When my form doesn't validate, I want to disable the form's submit function. I can do this by disabling the submit button, but this would leave the enter key unchecked.
I can set the .submit(return false); but then I can't re-enable it again later.

当我的表单未通过验证时,我想禁用表单的提交功能。我可以通过禁用提交按钮来做到这一点,但这会使输入键处于未选中状态。
我可以设置 .submit(return false); 但后来我无法再次启用它。

Any suggestions?

有什么建议?

回答by Nick Franceschina

just a simplification of Pim's (edited to pull up James contribution):

只是 Pim 的简化(编辑以提取 James 的贡献):


$('#form').submit(formValidated);

回答by Alexander Taubenkorb

I did the following, which worked (disable submit button and form submit):

我做了以下工作(禁用提交按钮和表单提交):

var condition = true; //check whatever you like
if(condition)
    $('input[type=submit]', this).attr('disabled', 'disabled');
    $('form').bind('submit',function(e){e.preventDefault();});
}else{
    $('input[type=submit]', this).removeAttr('disabled', 'disabled');
    $('form').unbind('submit');
}

回答by Pim Jager

How about doing the check inside the submit method:

如何在 submit 方法中进行检查:

$('#form').submit (function() {
 if(formValidated())
  return true;
 return false;
});

回答by theoski

Just a simplification of Nick's originalanswer:

只是尼克原始答案的简化:

$('#form').submit(formValidated);

回答by karim79

Here's another possible solution:

这是另一种可能的解决方案:

$('#form').change(function() {
    (isValid($('#form')) ? $('#submitBtn').attr('disabled',false) : $('#submitBtn').attr('disabled',true)
});

Of course, isValid()checks that all your $('#form')elements comply to your validation rules. That's how I would do it.

当然,isValid()检查您的所有$('#form')元素是否符合您的验证规则。这就是我要做的。

回答by Hawk Kroeger

Instead of directly calling the submit function call an intermediary function and do a check in there.

不是直接调用提交函数,而是调用一个中间函数并在那里进行检查。

i.e.

IE

var isSubmitable = false;

function submitForm() {
   if(isSubmitable) {
       myform.submit();
   }
}