Javascript jQuery 验证();仅在单击一种类型的提交按钮时进行验证

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

jQuery validate(); Only validate on click of one type of submit button

javascriptjqueryjquery-validatevalidation

提问by Will Hancock

I have a quite perflexing problem that I have researched and come up blank;

我有一个相当复杂的问题,我已经研究过,但结果是空白;

Scenario:I have a form, with the jQuery bassistance.de's validate() plugin (http://docs.jquery.com/Plugins/Validation).

场景:我有一个表单,带有 jQ​​uery bassistance.de 的 validate() 插件 ( http://docs.jquery.com/Plugins/Validation)。

Does the job on submit perfectly, however; I two types of submit button.

然而,提交的工作是否完美;我有两种提交按钮。

One submits the form as usual, triggering my back-end validation and return errors or success.

一个人像往常一样提交表单,触发我的后端验证并返回错误或成功。

The other type, is to all intense and purposes a submit button to the browser, however it tells my back-end to add another section of inputs (ie, a repeating element for employment history.) I want the user to be able to add the repeating elements without having to complete the rest of the form first. So clicking the second type of submit I want to BYPASS the validate();

另一种类型是所有强烈的和目的是向浏览器提交按钮,但是它告诉我的后端添加另一部分输入(即,就业历史的重复元素。)我希望用户能够添加重复元素,而不必先完成表单的其余部分。所以点击第二种提交我想绕过validate();

HTML:

HTML:

<form method="post" action="" id="ApplicationForm" name="ApplicationForm">

  <input id="job1" name="job1" class="required"/>
  <input type="submit" name="delete_job1" value="Delete" class="delete" />

  <input id="job2" name="job2" class="required"/>
  <input type="submit" name="delete_job2" value="Delete" class="delete" />

  <input type="submit" name="add_job" value="Add Job" class="add" />

  <input type="submit" name="ApplicationForm" value="Save Details" class="submit" />

</form>

I have tried a listener for the true submit button below, but this just stops the validate all togerther;

我已经为下面的真正提交按钮尝试了一个监听器,但这只会一起停止验证;

$(document).ready(function()
{
    var submitPress

    $(".submit").click(function()
    {
        submitPress = true;
    });

    if (submitPress)
    {
        $('form#ApplicationForm').validate();
    }
});

I guess this is because the validate(); listeners is setup on the initial loading of the page, not upon submit...

我想这是因为 validate(); 侦听器是在页面初始加载时设置的,而不是在提交时设置...

Is there there a rule or setting for validate(); I am missing, that I could utilise, or even a good hack with good old fasioned JS.

validate() 是否有规则或设置;我错过了,我可以利用,甚至是一个很好的老式 JS 黑客。

Muchos gratias who ever can help, i'm at my whits end.

任何人都可以提供帮助,我已经无能为力了。

Yours Sincerely, Working Late, AGAIN , Developer

此致,工作到很晚,再次,开发人员

回答by goat

http://docs.jquery.com/Plugins/Validation/Reference#Skipping_validation_on_submit

http://docs.jquery.com/Plugins/Validation/Reference#Skipping_validation_on_submit

basically, add class="cancel"to the submit buttons which you don't want to trigger validation.

基本上,添加class="cancel"到您不想触发验证的提交按钮。

<input type="submit" value="I Validate" />
<input type="submit" class="cancel" value="I skip Validation" />

回答by Ankit Agrawal

its not working,because the id you have provided of your input element,that become change like this submit_1_btnSubmit,after page load

它不起作用,因为您提供的输入元素的 idsubmit_1_btnSubmit在页面加载后变成这样

so to get rid this error you may use ClientIDMode="Static"

所以为了摆脱这个错误,你可以使用 ClientIDMode="Static"

(if you are working on asp.net)
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" CssClass="Submit" ClientIDMode="Static" />

else

you may access `clientIDmode`  property on button control dynamically.