Jquery function BEFORE form submission

后端 未结 7 2181
一整个雨季
一整个雨季 2020-12-13 08:25

I am trying to fire a function using Jquery when the form submit button is clicked, but the function needs to fire BEFORE the form is actually submitted.

I am trying

7条回答
  •  萌比男神i
    2020-12-13 08:38

    Based on Wakas Bukhary answer, you could make it async by puting the last line in the response scope.

    $('#myform').submit(function(event) {
    
      event.preventDefault(); //this will prevent the default submit
      var _this = $(this); //store form so it can be accessed later
    
      $.ajax('GET', 'url').then(function(resp) {
    
        // your code here 
    
       _this.unbind('submit').submit(); // continue the submit unbind preventDefault
      })  
    }
    

提交回复
热议问题