How can I correctly capture and re-fire the form submit event, guaranteed?

前端 未结 4 1051
名媛妹妹
名媛妹妹 2021-02-07 03:23

This is probably not your usual \"How do I capture form submit events?\" question.

I\'m trying to understand precisely how form submit events are handled by jQu

4条回答
  •  有刺的猬
    2021-02-07 03:44

    Bind to the form's submit handler with jQuery and prevent the default action, then, when you want to submit the form, trigger it directly on the form node.

    $("#formid").submit(function(e){
        // prevent submit
        e.preventDefault();
    
        // validate and do whatever else
    
    
        // ...
    
    
        // Now when you want to submit the form and bypass the jQuery-bound event, use 
        $("#formid")[0].submit();
        // or this.submit(); if `this` is the form node.
    
    });
    

    By calling the submit method of the form node, the browser does the form submit without triggering jQuery's submit handler.

提交回复
热议问题