How to use confirm using sweet alert?

后端 未结 7 1889
萌比男神i
萌比男神i 2020-12-05 00:11

In this code form is submitted even i am clicking on no

document.querySelector(\'#from1\').onsubmit = function(){

 swal({
    title: \"Are you sure?\",
             


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 00:52

    100% working

    Do some little trick using attribute. In your form add an attribute like data-flag in your form, assign "0" as false.

    //your inputs

    In your javascript:

    document.querySelector('#from1').onsubmit = function(e){
    
        $flag = $(this).attr('data-flag');
    
        if($flag==0){
            e.preventDefault(); //to prevent submitting
    
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this imaginary file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes, I am sure!',
                cancelButtonText: "No, cancel it!",
                closeOnConfirm: false,
                closeOnCancel: false
             },
             function(isConfirm){
    
               if (isConfirm){
                    swal("Shortlisted!", "Candidates are successfully shortlisted!", "success");
    
                    //update the data-flag to 1 (as true), to submit
                    $('#from1').attr('data-flag', '1');
                    $('#from1').submit();
                } else {
                    swal("Cancelled", "Your imaginary file is safe :)", "error"); 
                }
             });
        }
    
        return true;
    });
    

提交回复
热议问题