jQuery validation conflicts with Ajax form submission [duplicate]

99封情书 提交于 2019-12-13 09:55:28

问题


Possible Duplicate:
jQuery validation: some of the required field not filled but the form can still be submitted

I used jquery validation (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) for form validation:

HTML:

<form id="formID" action="/processForm" method="post">
  ...
  <input type="submit" value="Submit" />
</form>

jQuery:

$("#formID").validate({
  onkeyup:false,
  rules: {
    ...
  },
  messages: {
    ...
  }
});


// Submit the form by Ajax
$(document).ready(function() {
  $('.formID').ajaxForm({
    success: function(returnData) {
      $('#content').html(returnData);
    }
  });
});

Now the form seems to be submitted by the validate() function, not by the Ajax function.

How do I still use the Ajax function?


回答1:


Now the form seems to be submitted by the validate() function, not by the Ajax function. How do I still use the Ajax function?

That's because the validate() plugin already has a submitHandler: built-in. Here's how to use it properly and avoid potential conflicts between multiple functions acting on the same submit event.

$(document).ready(function() {

    $("#formID").validate({
      onkeyup:false,
      rules: {
        ...
      },
      messages: {
        ...
      },
      submitHandler: function(form) {
          form.ajaxForm({
              success: function(returnData) {
                  $('#content').html(returnData);
              }
          });
      }
    });

});



回答2:


try this JS:

$('#formID').on('submit',function(){

// call your function which send AJAX  reuest
// Or code of sending ajax reauest

return false;

})


来源:https://stackoverflow.com/questions/14321692/jquery-validation-conflicts-with-ajax-form-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!