jQuery: Stop submitting form, perform script, continue submitting form?

前端 未结 8 1045
遇见更好的自我
遇见更好的自我 2021-02-04 02:09

I have a form, and when I submit him I execute multiple script. Here is my code:

$(\"#RequestCreateForm\").submit(function (e) {
    if ($(\"#RequestCreateForm\"         


        
相关标签:
8条回答
  • 2021-02-04 02:44

    When you call $("#RequestCreateForm").submit(), the script will just run through the event handler again, and cause an infinite loop (as Koen pointed out in a comment on the accepted answer). So, you need to remove the event handler before submitting:

    $("#RequestCreateForm").on('submit', function (e) {
        e.preventDefault();
        // do some stuff, and if it's okay:
        $(this).off('submit').submit();
    });
    

    The last line needs to be in a conditional statement, otherwise it'll just always happen, and negate your e.preventDefault(); at the top.

    0 讨论(0)
  • 2021-02-04 02:48
    
    $("#RequestCreateForm").submit(function (e) {
        if ($("#RequestCreateForm").validate().checkForm() === false) { 
           e.preventDefault(); 
           //form was NOT ok - optionally add some error script in here
    
           return false; //for old browsers 
        } else{
           //form was OK - you can add some pre-send script in here
        }
    
        //$(this).submit(); 
        //you don't have to submit manually if you didn't prevent the default event before
    }
    
    0 讨论(0)
  • 2021-02-04 02:50
    $("#RequestCreateForm").submit(function (e) {
        if ($("#RequestCreateForm").validate().checkForm() == false) 
        { 
             e.preventDefault();
             return false; 
        }        
    
        //other scripts
    
    }
    
    0 讨论(0)
  • 2021-02-04 02:53
    $("#RequestCreateForm").submit(function (e) {
        if ($("#RequestCreateForm").validate().checkForm() == false) { return; }
    
        e.preventDefault();
    
        //many scripts
    
        // Bypass the jquery form object submit and use the more basic vanilla
        // javascript form object submit
    
        $("#RequestCreateForm")[0].submit();
    
    }
    
    0 讨论(0)
  • 2021-02-04 02:55

    Here is my approach to avoid the infinite loop.

    • In the form, I use a "button" with an id (e.g. <input type="button" id="submit" value="submit"/>) to mimic the submit button;
    • In the script I have something like this:
    $('#submit').click(function() {
      if(//validation rules is ok)
      {
          $("#RequestCreateForm").submit(); //assuming the form id is #RequestCreateForm
      }
      else
      {
          return false;
      }
    });
    
    0 讨论(0)
  • 2021-02-04 02:56

    All solutions here are too complicated or lead to javascript error, simpliest and clearest solution I guess:

    jQuery("#formid").submit(function(e) {
            if( ! (/*check form*/)  ){  //notice the "!"
              e.preventDefault();
              //a bit of your code
            } //else do nothing, form will submit
    }); 
    
    0 讨论(0)
提交回复
热议问题