Can JQuery.Validate plugin prevent submission of an Ajax form

后端 未结 7 1786
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 15:00

I am using the JQuery form plugin (http://malsup.com/jquery/form/) to handle the ajax submission of a form. I also have JQuery.Validate (http://docs.jquery.com/Plugins/Validatio

相关标签:
7条回答
  • 2021-02-02 15:22

    ok, this is an old question but i will put our solution here for posterity. i personally classify this one as a hack-trocity, but at least it's not a hack-tacu-manjaro

    <script type="text/javascript">
        var options = {
            target: '#detailsView'
        };
    
    // -- "solution" --
    $('#searchForm').submit(function(event) {
      this.preventDefault(event); // our env actually monkey patches preventDefault
                                  // impl your own prevent default here
                                  // basically the idea is to bind a prevent default
                                  // stopper on the form's submit event
    });
    // -- end --
    
        $('#searchForm').ajaxForm(options);
    
        $('#searchForm').validate({
          rules: {
          username: {required:true}},
          messages: {
          username: {required:"Username is a required field."}}
        });
    </script>
    
    0 讨论(0)
  • 2021-02-02 15:24

    ... well it's been a while so my situation has changed a little. Currently I have a submitHandler option passed to the Validate() plugin. In that handler I manually use ajaxSubmit. More a workaround than an answer I guess. Hope that helps.

    http://jquery.bassistance.de/validate/demo/ajaxSubmit-intergration-demo.html

    var v = jQuery("#form").validate({
        submitHandler: function(form) {
            jQuery(form).ajaxSubmit({target: "#result"});
        }
    });
    
    0 讨论(0)
  • 2021-02-02 15:29

    Just as a first pass, I'm wondering why the line

    $("form").validate({
    

    doesn't refer to $("searchform"). I haven't looked this up, or tried it, but that just seems to be a mismatch. Wouldn't you want to call validate on the appropriate form?

    Anyway, if this is completely wrong, then the error isn't immediately obvious. :)

    0 讨论(0)
  • 2021-02-02 15:30

    Also make sure all of your input fields have a "name" attribute as well as an "id" attribute. I noticed the jquery validation plugin doesn't function correctly without these.

    0 讨论(0)
  • 2021-02-02 15:35
    $('#contactform').ajaxForm({
      success : FormSendResponse, 
      beforeSubmit:  function(){
        return $("#contactform").valid();
      }
    });
    
    
    $("#contactform").validate();   
    

    Above code worked fine for me.

    0 讨论(0)
  • 2021-02-02 15:35

    May be a return false; on the form will help? :) I mean:

    <form id="searchForm" method="post" action="/User/GetDetails" onSubmit="return false;">
    
    0 讨论(0)
提交回复
热议问题