简体   繁体   中英

How to stop form from submitting using Jquery/Ajax

This is the code I have. I want to prevent the form from submitting if the message i receive from the php side is 0 (an error occured). If the message received is 1 I want to submit the form.

   $(document).ready(function(){
    $('#form').submit(function(e){
        register();


    });

});


    function register(){

        $.ajax({
            type:"POST",
            url:"submit.php",
            data: $('#form').serialize(),
            dataType: "json",
            success: function(msg){

                if(parseInt(msg)==1){
                    window.location=msg;
                }
                else if(parseInt(msg)==0){  
                    $('#error_out').html(msg);
                }


            }

        });
    }

after register(); include return false;

 $('#form').submit(function(e){
           e.preventDefault();

           //do whatever you want

});

after register(); include return false; if it doesn't work then rename register(); to register(e); and then include e.preventDefault(); e.stopPropagation(); e.preventDefault(); e.stopPropagation(); hope it works.

As I understand,Try use this:

if(parseInt(msg)==0){ $('#submit').attr('disabled','true'); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM