jQuery .validate()submitHandler没有触发

dav*_*d-l 14 jquery jquery-validate bootbox

我正在加载一个对话框,其中包含使用Bootbox.js即时构建的表单,我正在使用jQuery validate插件验证用户输入.

验证工作正常,但submitHandler在成功填写表单时会被忽略.

出了什么问题?

submitHandler: function(form) {
    alert("Submitted!");
    var $form = $(form);
    $form.submit();
}
Run Code Online (Sandbox Code Playgroud)

请参阅下面的完整示例.我查看了其他提出类似问题的帖子.不幸的是,他们似乎在页面上呈现了表单,而我正在通过jQuery渲染.

$(document).on("click", "[data-toggle=\"contactAdmin\"]", function() {
  bootbox.dialog({
    title: "Contact admin",
    buttons: {
      close: {
        label: 'Close',
        className: "btn btn-sm btn-danger",
        callback: function() {}
      },
      success: {
        label: "Submit",
        className: "btn btn-sm btn-primary",
        callback: function() {
          $("#webteamContactForm").validate({
            rules: {
              requestType: {
                required: true
              }
            },
            messages: {
              requestType: {
                required: "Please specify what your request is for",
              }
            },
            highlight: function(a) {
              $(a).closest(".form-group").addClass("has-error");
            },
            unhighlight: function(a) {
              $(a).closest(".form-group").removeClass("has-error");
            },
            errorElement: "span",
            errorClass: "help-blocks",
            errorPlacement: function(error, element) {
              if (element.is(":radio")) {
                error.appendTo(element.parents('.requestTypeGroup'));
              } else { // This is the default behavior 
                error.insertAfter(element);
              }
            },
            submitHandler: function(form) {
              alert("Submitted!");
              var $form = $(form);
              $form.submit();
            }
          }).form();
          return false;
        }
      }
    },
    message: '<div class="row">  ' +
      '<div class="col-md-12"> ' +
      '<form id="webteamContactForm" class="form-horizontal" method="post"> ' +
      '<p>Please get in touch if you wish to delete this content</p>' +
      '<div class="form-group"> ' +
      '<div class="col-md-12"> ' +
      '<textarea id="message" name="message" class="form-control input-md" rows="3" cols="50">This email is to notify you the creator is putting a request for the following item\n\n' +
      this.attributes.getNamedItem("data-url").value + '\n\n' + '</textarea> ' +
      '<span class="help-block">Feel free to change the message and add more information. Please ensure you always provide the link.</span> </div> ' +
      '</div> ' +
      '<div class="form-group requestTypeGroup"> ' +
      '<label class="col-md-4 control-label" for="requestType">This request is for:</label> ' +
      '<div class="col-md-4"> <div class="radio"> <label for="Edit"> ' +
      '<input type="radio" name="requestType" id="requestType-0" value="Edit"> ' +
      'Editing </label> ' +
      '</div><div class="radio"> <label for="Delete"> ' +
      '<input type="radio" name="requestType" id="requestType-1" value="Delete"> Deletion</label> ' +
      '</div> ' +
      '</div> </div>' +
      '</form> </div>  </div>'
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.js"></script>

<a data-toggle="contactAdmin" data-title="help" data-url="http:/www.mydomain.com/some-url" href="#">Contact Web team</a>
Run Code Online (Sandbox Code Playgroud)

查看jsFiddle

Spa*_*rky 24

检查jsFiddle的DOM和两个问题变得明显.

  1. 你的"提交" <button>是一个type="button".

  2. 您的"提交"按钮位于<form></form>容器之外.

如果你想让jQuery Validate插件自动捕获click"提交"按钮的事件......

  • 按钮必须是 type="submit"
  • 按钮必须位于<form></form>容器内.

如果您希望插件按预期运行,则必须满足这两个条件.


您还错误地将.validate()方法放在success模式对话框"提交"按钮的回调中.

.validate()方法仅用于初始化插件,应在创建表单后调用一次.


编辑:

玩完这个之后,很明显Bootbox模式插件可能有一些严重的限制,干扰了表单的提交.

  1. 我打开对话框初始化Validate插件.

  2. .valid()在"提交"中使用该方法来触发验证测试.

我可以初始化验证并按预期工作,但在实际表单提交之前,对话框将被取消.也许有一个解决方案,但在审查了Bootbox的文档后,它并不是很明显.

https://jsfiddle.net/vyaw3ucd/2/


编辑2:

根据OP的解决方案......

bootbox.dialog({
    // other options,
    buttons: {
        success: {
            label: "Submit",
            className: "btn btn-sm btn-primary",
            callback: function () {
                if ($("#webteamContactForm").valid()) {
                    var form = $("#webteamContactForm");
                    form.submit();  // form submits and dialog closes
                } else {
                    return false;  // keeps dialog open
                }
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

但是,通过直接使用提供的form参数,在使用submitHandlerjQuery Validate插件的选项时没有任何错误.

submitHandler: function (form) {
    console.log("Submitted!");
    form.submit();
}
Run Code Online (Sandbox Code Playgroud)

演示:https://jsfiddle.net/vyaw3ucd/5/