简体   繁体   中英

jquery show a confirm alert before submitting

jQuery('document').ready(function(){
jQuery('#survey').on('click', function(){
    var postData = jQuery('#surveyForm').serialize();

    jQuery.post('survey.php', postData, function(data){
        alert(data);
     if($.trim(data) == "Your Score sent Successfully!") {
            location.reload();  
        }

    });     
});

});

this is my jquery code, can any one please help me how to add a confirm alert "are u sure you want to submit" in the above code. Thanks in advance.

You can use Javascript confirm box as shown :-

jQuery('document').ready(function(){
   jQuery('#survey').on('click', function(){
     if(confirm("Do You want to save?")){
       var postData = jQuery('#surveyForm').serialize();

       jQuery.post('survey.php', postData, function(data){
         alert(data);
         if($.trim(data) == "Your Score sent Successfully!") {
            location.reload();  
         }
       });   
     }  
   });
});

DEMO

i think in your code response is not come from php file check it. and try this add your code in between confirm condition

 jQuery('document').ready(function(){ jQuery('#survey').on('click', function(){ if(confirm("Do You want to save?")){ // var postData = jQuery('#surveyForm').serialize(); // // jQuery.post('survey.php', postData, function(data){ // alert(data); // if($.trim(data) == "Your Score sent Successfully!") { location.reload(); // } // }); } }); }); 

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