只有在表单验证时才会发生某些事情

时间:2014-03-15 23:21:57

标签: javascript jquery forms jquery-validate

如何在表单验证时调用函数?

我的提交按钮会在按下时打开一个模态,但即使表单未验证,它也会执行此操作。如何将模态与验证方面联系起来,以便模式仅在表单有效时显示?

HTML:

<head>
   <script src="js/additional-methods.js"></script>
   <script src="js/jquery-validate.min.js"></script>
   <script src="js/modalEffects.js"></script>

                                                                                                                                                              

            <div class="md-modal md-effect-16" id="modal-16">
               <div class="md-content">
                   <h3>Thank you for signing up!</h3>
                   <div>
                       <p>We're excited to have you onboard as launch in late July.</p>
                       <ul>
                           <li><strong>Read:</strong> Check your email. Make sure you're subscribed to the email list.</li>
                           <li><strong>Surprise:</strong> Thanks for believing in us. As a gesture of thanks, you're getting the first batch of $50 giftcards to use on VERSEUX.</li>
                           <li><strong>Share:</strong> There are still some giftcards left. Share them with your friends, family, and fashionistas.</li>
                       </ul>
                       <button class="md-close">Close me!</button>
                   </div>
               </div>
           </div>
           <div class="md-overlay"></div><!-- the overlay element -->

   <script src="js/classie.js"></script>
   <script src="js/modalEffects.js"></script>

JSfiddle

JS和CSS代码很长,所以它是小提琴

1 个答案:

答案 0 :(得分:3)

引用OP:

  

&#34;我的提交按钮会在按下时打开一个模态,但即使表单未验证,它也会执行此操作。如何将模态与验证方面联系起来,以便模式仅在表单有效时显示?&#34;

在jQuery Validate插件中,submitHandler回调函数仅在表单有效时单击提交按钮时触发。因此,您可以从内部触发模态的打开。

请参阅:http://jqueryvalidation.org/validate

但是,尽管您已在代码中包含了jQuery Validate插件文件,但您并未在OP或jsFiddle中使用.validate()方法。