【问题标题】:How to get reference to submit button using jQuery validation plugin?如何使用 jQuery 验证插件获取提交按钮的引用?
【发布时间】:2014-05-11 17:44:18
【问题描述】:

如何使用 jQuery 验证插件获取对提交按钮的引用?

function defSubmHendler(){
    $myform.validate({
        rules: rules,
        messages: messages,
        submitHandler: function() {
            event.preventDefault();
            if (~this form id == A){
                condition1;
            }
            else if (~this form id == B){
                condition2;
            } else {}
            ...
            $.ajax({
                ...
            });
        }
    });
}

在这种情况下 this == $.validator。我需要在 submitHandler 中引用提交按钮/表单。

【问题讨论】:

  • 你怎么打电话给defSubmitHandler
  • 动态创建表单,然后执行 defSubmHendler('destination_form')

标签: javascript jquery validation


【解决方案1】:

考虑为您的提交按钮提供id 并像这样引用它:

submitHandler: function() {
    //Your submit button referenced by its ID
    $("#mySubmitButton").doStuff(); 
}

或者,使用 submitHandler 函数中的 form 元素并找到该表单中包含的提交按钮:

submitHandler: function(form) {
   //Via the form argument passed to the submitHandler 
   var btnSubmit = form.find(":submit");
}

【讨论】:

  • 表单是动态创建的。处理程序不知道这个按钮属于什么形式。我的例子可能不正确。 $myform 是可变的
猜你喜欢
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 2010-09-21
相关资源
最近更新 更多