如何使 Jquery 表单验证函数在其他函数之前执行/触发?

发布于 2024-08-19 03:50:21 字数 963 浏览 5 评论 0原文

我使用 Jquery 表单验证来验证表单输入数据。有一个

确认

检查

提交 这种形式的。 .

代码是:

<script type="text/javascript">
function Confirmation(){
    var answer = confirm("Do you really want to withdraw this amount of money from your account?")
    if (answer){
        return true;
    }
    else{
        return false;
    }

}
$(document).ready(function() { 
$("#withdraw").validate({ 
        rules: { 
         amount: {
            required: true,
             digits:true


        } ,
        bank:{
            required:true,

        },
        cardnumber1: {
            required: true,
             minlength:8

        },
       cardnumber2:{
          required:true,
          equalTo: "#cardnumber1"
         },
          holder:{
          required:true,
        }
  }
})
}); 
</script>

我想在Confirmation()之前执行Jquery表单验证,该怎么做?

I use Jquery form validation to validate form input data. There is a

confirm

check on

submit
of this form.
.

The code is:

<script type="text/javascript">
function Confirmation(){
    var answer = confirm("Do you really want to withdraw this amount of money from your account?")
    if (answer){
        return true;
    }
    else{
        return false;
    }

}
$(document).ready(function() { 
$("#withdraw").validate({ 
        rules: { 
         amount: {
            required: true,
             digits:true


        } ,
        bank:{
            required:true,

        },
        cardnumber1: {
            required: true,
             minlength:8

        },
       cardnumber2:{
          required:true,
          equalTo: "#cardnumber1"
         },
          holder:{
          required:true,
        }
  }
})
}); 
</script>

I want the Jquery form validation is executed before the Confirmation(), how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨夜星沙 2024-08-26 03:50:21

Steven,

您需要从表单中删除 onsubmit 处理程序,并将其添加到验证插件的 submitHandler 中......

$("#withdraw").validate({
        rules: {
           amount: {
            required: true,
             digits:true
                   }
             // .. other rules here
               },
        submitHandler: function(form){
         if( Confirmation() )
             form.submit();
  }
})

Steven,

You need to remove the onsubmit handler from the form and add it the the submitHandler of the validate plugin ...

$("#withdraw").validate({
        rules: {
           amount: {
            required: true,
             digits:true
                   }
             // .. other rules here
               },
        submitHandler: function(form){
         if( Confirmation() )
             form.submit();
  }
})
︶葆Ⅱㄣ 2024-08-26 03:50:21

我查看了文档,有一个名为submitHandler的选项,我认为它会允许您在提交表单之前但在验证发生之后添加确认对话框。试试这个...

<script type="text/javascript">
$(document).ready(function() { 
  $("#withdraw").validate({ 
    submitHandler : function(form) {
      if (confirm("Do you really want to withdraw this amount of money from your account?")) {
        form.submit();
      }
    },
    rules: { 
      amount: {
        required: true,
        digits:true
      },
      bank:{
        required:true,
      },
      cardnumber1: {
        required: true,
        minlength:8
      },
      cardnumber2:{
        required:true,
        equalTo: "#cardnumber1"
      },
      holder:{
        required:true,
      }
    }
  })
}); 
</script>

I looked at the documentation and there is an option named submitHandler which I think would allow you to add your confirmation dialog before the form is submitted but after the validation happens. Try this...

<script type="text/javascript">
$(document).ready(function() { 
  $("#withdraw").validate({ 
    submitHandler : function(form) {
      if (confirm("Do you really want to withdraw this amount of money from your account?")) {
        form.submit();
      }
    },
    rules: { 
      amount: {
        required: true,
        digits:true
      },
      bank:{
        required:true,
      },
      cardnumber1: {
        required: true,
        minlength:8
      },
      cardnumber2:{
        required:true,
        equalTo: "#cardnumber1"
      },
      holder:{
        required:true,
      }
    }
  })
}); 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文