【问题标题】:Perform jQuery only if form is valid仅当表单有效时执行 jQuery
【发布时间】:2011-11-02 05:58:27
【问题描述】:

我正在使用 jQuery 验证插件,并且还使用 jQuery 为我的表单所在的弹出窗口制作关闭动画。

当然,如果表单无效并且需要修复错误,我不希望弹出窗口关闭。

这就是我正在使用的...

表单验证:

$(document).ready(function(){
    $("#form").validate({
       errorContainer: "#messageBox1",
       errorLabelContainer: "#messageBox1 ul",
       wrapper: "li",
    });
    });

提交表单按钮操作:

$("#ContinueButton").click(function(){
$("#refer-a-friend").animate({
    top: "0%",
    opacity: 0
}, 500 );
});

那么我该如何制作才能使关闭动画仅在表单有效时才触发?

【问题讨论】:

    标签: jquery forms validation


    【解决方案1】:

    您的验证将在#form 提交时运行。您的#ContinueButton 需要是type="submit",或致电$("#form").submit();

    一旦你有按钮提交触发验证,看看documentation中的submitHandler选项,它应该是你需要的。像这样的:

    $("#form").validate({
        submitHandler: function(form) {
          //perform your animation here 
       }
    });
    

    【讨论】:

      【解决方案2】:
      $("#ContinueButton").click(function(){
         if($("#form").valid()) {
            $("#refer-a-friend").animate({
               top: "0%",
               opacity: 0
             }, 500 );
         }
      });
      

      http://docs.jquery.com/Plugins/Validation/Validator

      http://docs.jquery.com/Plugins/Validation/valid

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-01
        • 1970-01-01
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多