Jquery function BEFORE form submission

后端 未结 7 2083
一整个雨季
一整个雨季 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条回答
  • 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
      })  
    }
    
    0 讨论(0)
  • 2020-12-13 08:42
    $('#myform').submit(function() {
      // your code here
    })
    

    The above is NOT working in Firefox. The form will just simply submit without running your code first. Also, similar issues are mentioned elsewhere... such as this question. The workaround will be

    $('#myform').submit(function(event) {
    
     event.preventDefault(); //this will prevent the default submit
    
      // your code here (But not asynchronous code such as Ajax because it does not wait for a response and move to the next line.)
      
     $(this).unbind('submit').submit(); // continue the submit unbind preventDefault
    })
    
    0 讨论(0)
  • 2020-12-13 08:43

    Just because I made this mistake every time when using the submit function.

    This is the full code you need:

    Add the id "yourid" to the HTML form tag.

    <form id="yourid" action='XXX' name='form' method='POST' accept-charset='UTF-8' enctype='multipart/form-data'>
    

    the jQuery code:

    $('#yourid').submit(function() {
      // do something
    });
    
    0 讨论(0)
  • 2020-12-13 08:47

    You can do something like the following these days by referencing the "beforeSubmit" jquery form event. I'm disabling and enabling the submit button to avoid duplicate requests, submitting via ajax, returning a message that's a json array and displaying the information in a pNotify:

    jQuery('body').on('beforeSubmit', "#formID", function() {
        $('.submitter').prop('disabled', true);
        var form = $('#formID');
        $.ajax({
            url    : form.attr('action'),
            type   : 'post',
            data   : form.serialize(),
            success: function (response)
            {
                response = jQuery.parseJSON(response);
                new PNotify({
                    text: response.message,
                    type: response.status,
                    styling: 'bootstrap3',
                    delay: 2000,
                });
                $('.submitter').prop('disabled', false);
            },
            error  : function ()
            {
                console.log('internal server error');
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-13 08:50

    You can use the onsubmit function.

    If you return false the form won't get submitted. Read up about it here.

    $('#myform').submit(function() {
      // your code here
    });
    
    0 讨论(0)
  • 2020-12-13 08:58

    Aghhh... i was missing some code when i first tried the .submit function.....

    This works:

    $('#create-card-process.design').submit(function() {
        var textStyleCSS = $("#cover-text").attr('style');
        var textbackgroundCSS = $("#cover-text-wrapper").attr('style');
        $("#cover_text_css").val(textStyleCSS);
        $("#cover_text_background_css").val(textbackgroundCSS);
    });
    

    Thanks for all the comments.

    0 讨论(0)
提交回复
热议问题