Using JQuery - preventing form from submitting

后端 未结 13 2945
灰色年华
灰色年华 2020-11-22 06:51

How do I prevent a form from submitting using jquery?

I tried everything - see 3 different options I tried below, but it all won\'t work:

    $(docu         


        
13条回答
  •  孤城傲影
    2020-11-22 07:29

    You probably have few forms o the page and using $('form').submit() adds this event to the first occurrence of the form on your page. Use class selector instead, or try this:

    $('form').each(function(){
        $(this).submit(function(e){
            e.preventDefault();
            alert('it is working!');
            return  false;
        })
    }) 
    

    or better version of it:

    $(document).on("submit", "form", function(e){
        e.preventDefault();
        alert('it works!');
        return  false;
    });
    

提交回复
热议问题