javascript 表单未通过 jQuery 验证提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7917128/
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-10-26 01:42:12  来源:igfitidea点击:

Form not getting submitted with jQuery validation

javascriptjqueryformsasp.net-mvc-3jquery-validate

提问by Maven

Ive got a little problem while using jQuery validation plugin in on my .net/MVC projects: The validation generally works all fine, the issue arises at the time of submitting: I want form not be submitted when there are any validation errors in it and for this I set debug:true;

我在我的 .net/MVC 项目中使用 jQuery 验证插件时遇到了一个小问题:验证通常都可以正常工作,问题出现在提交时:当表单中存在任何验证错误时,我希望不提交表单和为此,我设置了 debug:true;

But after setting it to true when the submit button becomes unresponsive, even when everything is fine i.e. no validation errors, nothing just happens while clicking the submit button. And if remove debug:true it submits the form even when there are validation errors in it. I want a solution to this problem, the middle way i.e.: When clicking submit if there are errors it doesn't submits the form and shows errors And if there are no errors then it just simple submits it.

但是在提交按钮无响应时将其设置为 true 后,即使一切正常,即没有验证错误,单击提交按钮时也不会发生任何事情。如果删除 debug:true 它会提交表单,即使其中存在验证错误。我想要一个解决这个问题的方法,中间方式,即:当点击提交时,如果有错误,它不会提交表单并显示错误,如果没有错误,那么它只是简单地提交它。

Help plz .. Thank you!

请帮忙.. 谢谢!

    <script type="text/javascript">
      $(document).ready(function() {
$("#productSubmit").validate( {
  debug : true, rules : {
     prdctttle : {
        maxlength : 50, 
        required : true, 
        onlyChars : true }
     , prdctdscrptn : {
        maxlength : 250, 
        required : true, 
        onlyChars : true }
    }
  , messages : {
     }
  }
); 
}
); 
$.validator.addMethod('onlyChars', function (value) {
return /^[a-zA-Z ,-!]+$/.test(value); }, 'Please enter a valid name with only alphabets');
   $("#productSubmit").validate( {
  submitHandler : function(form) {
     if ($(form).valid()) form.submit(); return false; // prevent normal form posting
     }
  }
);
</script>

回答by GregL

Change your last line to check if the form is valid before submitting:

更改最后一行以在提交前检查表单是否有效:

$("#productSubmit").validate({ 
    submitHandler: function(form) { ?
                           if ($(form).valid()) 
                               form.submit(); 
                           return false; // prevent normal form posting
                    }
 });

EDIT:I forgot to add the "return false;" line to stop the form submitting normally (so it only happens when the validation passes).

编辑:我忘了添加“返回假;” 行停止表单正常提交(所以它只在验证通过时发生)。

回答by PenguinOfwar

GregL's answer is correct for the most part.

GregL 的回答大部分是正确的。

What ultimately got this working correctly for me was also the addition of:

最终让我正常工作的还有:

onsubmit: false

Final code:

最终代码:

$("#productSubmit").validate({ 
 onsubmit: false,
 submitHandler: function(form) {  
   if ($(form).valid())
   {
       form.submit(); 
   }
   return false; // prevent normal form posting
 }
});

This overrides the default submit behaviour, and handles the final validation check and submission within the submitHandler.

这会覆盖默认的提交行为,并在 submitHandler 中处理最终的验证检查和提交。