【问题标题】:JQuery: My form is submitting even though validate returns false [duplicate]JQuery:我的表单正在提交,即使验证返回 false [重复]
【发布时间】:2019-09-11 18:05:15
【问题描述】:

我有一个使用 JQuery Validator 的表单。如果未选中 2 个复选框,我会显示错误并且不应该提交表单。但我发现它无论如何都会提交,即使我的代码返回错误。这是我的 HTML 代码的一部分:

 <form id="addressForm" name="addressForm" method="post">
     <div class="alertBox infoAlert" style="padding-top:8px;padding-left:4px;">
         <div class="checkbox-inline">
              <label class="checkboxContainer">I have read and agree to the <a target="newtab" href="${(www_apa_org_url)!?string}/about/termsofuse">APA Terms and Conditions</a>
              <@spring.bind "shippingAddressForm.awtTermsAccepted"/>
              <input type="checkbox" name="${(spring.status.expression)!}" id="awtTermsAccepted" value="true">
               <span class="checkmark"></span>
              </label>
         </div>
         <div class="checkbox-inline" style="margin-left:0;">
             <label class="checkboxContainer">I have read and agree to the <a href="https://academicwriter.apa.org/about/terms" target="_blank">Academic Writer Terms of Service</a>
                <@spring.bind "shippingAddressForm.apaTermsAccepted"/>
                <input type="checkbox" name="${(spring.status.expression)!}" id="apaTermsAccepted" value="true">
                <span class="checkmark"></span>
            </label>
         </div>
         <div id="gdprerror" class="help-block hide" style="color: #900;">Please indicate that you agree to the terms and policies above by checking all two boxes.</div>
     </div><!-- /.alertBox -->
     <div class="col-md-6 col-md-push-6">
            <div class="alignRightThen100Width"><button onclick="submitAddressForm();" class="btn btn-cta-ecommerce fullWidthButton760" id="addressFormSubmit" style="min-width:350px;"/>Go to Payment &amp; Review<i class="fa fa-chevron-right" style="padding-left:6px;" aria-hidden="true"></i></button></div>
     </div>
</form>

这是 JS:

function beforeAddressPageSubmit(){
    if(showBillingAddress){
        removeState('Billing');
    }
}

function submitAddressForm() {
    var validator = jQuery("#addressForm").validate();
    var isValid = validator.form() && awtTermsAgreeCheckbox();
    if (isValid) {
        beforeAddressPageSubmit();
        jQuery("#addressForm").submit();
    }
}

是不是我做错了什么?

【问题讨论】:

  • 因为您有一个提交按钮并且您没有返回 false 或取消点击的默认操作

标签: javascript jquery


【解决方案1】:

您需要将type="button" 添加到您的按钮标签中。

function beforeAddressPageSubmit(){
    if(showBillingAddress){
        removeState('Billing');
    }
}

function submitAddressForm() {
    var validator = jQuery("#addressForm").validate();
    var isValid = validator.form() && awtTermsAgreeCheckbox();
    if (isValid) {
        beforeAddressPageSubmit();
        jQuery("#addressForm").submit();
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="addressForm" name="addressForm" method="post">
     <div class="alertBox infoAlert" style="padding-top:8px;padding-left:4px;">
         <div class="checkbox-inline">
              <label class="checkboxContainer">I have read and agree to the <a target="newtab" href="${(www_apa_org_url)!?string}/about/termsofuse">APA Terms and Conditions</a>
              <@spring.bind "shippingAddressForm.awtTermsAccepted"/>
              <input type="checkbox" name="${(spring.status.expression)!}" id="awtTermsAccepted" value="true">
               <span class="checkmark"></span>
              </label>
         </div>
         <div class="checkbox-inline" style="margin-left:0;">
             <label class="checkboxContainer">I have read and agree to the <a href="https://academicwriter.apa.org/about/terms" target="_blank">Academic Writer Terms of Service</a>
                <@spring.bind "shippingAddressForm.apaTermsAccepted"/>
                <input type="checkbox" name="${(spring.status.expression)!}" id="apaTermsAccepted" value="true">
                <span class="checkmark"></span>
            </label>
         </div>
         <div id="gdprerror" class="help-block hide" style="color: #900;">Please indicate that you agree to the terms and policies above by checking all two boxes.</div>
     </div><!-- /.alertBox -->
     <div class="col-md-6 col-md-push-6">
            <div class="alignRightThen100Width"><button type="button" onclick="submitAddressForm();" class="btn btn-cta-ecommerce fullWidthButton760" id="addressFormSubmit" style="min-width:350px;"/>Go to Payment &amp; Review<i class="fa fa-chevron-right" style="padding-left:6px;" aria-hidden="true"></i></button></div>
     </div>
</form>

【讨论】:

  • 技术上这个答案是正确的。
  • 我也不明白反对票...但它确实有效!你能告诉我为什么添加type=button 会改变页面的行为吗?
  • @user1660256 所以默认情况下表单会被提交。这是需要防止的,因为当出现错误时,甚至浏览器也不允许提交表单来显示错误。这是 HTML5 的新功能。
  • 因为默认按钮是 type="submit" 所以更改为 "button" 它将阻止默认提交表单
  • @user1660256 我会说这个答案是正确的,它解决了它。 ?
猜你喜欢
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
相关资源
最近更新 更多