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

前端 未结 8 1044
遇见更好的自我
遇见更好的自我 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.

提交回复
热议问题