jQuery - prevent default, then continue default

后端 未结 10 1549
[愿得一人]
[愿得一人] 2020-12-02 16:27

I have a form that, when submitted, I need to do some additional processing before it should submit the form. I can prevent default form submission behavior, then do my addi

10条回答
  •  爱一瞬间的悲伤
    2020-12-02 16:58

    Using this way You will do a endless Loop on Your JS. to do a better way you can use the following

    var on_submit_function = function(evt){
        evt.preventDefault(); //The form wouln't be submitted Yet.
        (...yourcode...)
    
        $(this).off('submit', on_submit_function); //It will remove this handle and will submit the form again if it's all ok.
        $(this).submit();
    }
    
    $('form').on('submit', on_submit_function); //Registering on submit.
    

    I hope it helps! Thanks!

提交回复
热议问题