How to use confirm using sweet alert?

后端 未结 7 1890
萌比男神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:33
    document.querySelector('#from1').onsubmit = function(e){
    
     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");
    
        } else {
          swal("Cancelled", "Your imaginary file is safe :)", "error");
             e.preventDefault();
        }
     });
    };
    
    0 讨论(0)
  • 2020-12-05 00:36

    inside your save button click add this code :

    $("#btnSave").click(function (e) {
      e.preventDefault();
      swal("Are you sure?", {
        buttons: {
          yes: {
            text: "Yes",
            value: "yes"
          },
          no: {
            text: "No",
            value: "no"
          }
        }
      }).then((value) => {
        if (value === "yes") {
          // Add Your Custom Code for CRUD
        }
        return false;
      });
    });
    
    0 讨论(0)
  • 2020-12-05 00:49

    document.querySelector('#from1').onsubmit = function(e){
    
     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.value){
         swal("Shortlisted!", "Candidates are successfully shortlisted!", "success");
    
        } else {
          swal("Cancelled", "Your imaginary file is safe :)", "error");
             e.preventDefault();
        }
     });
    };

    0 讨论(0)
  • 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.

    <form id="from1" data-flag="0">
        //your inputs
    </form>
    

    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;
    });
    
    0 讨论(0)
  • 2020-12-05 00:56

    You will need to prevent default form behaviour on submit. After that you will need to submit form programmatically in case of Ok button is selected.

    Here is how it could look like:

    document.querySelector('#from1').addEventListener('submit', function(e) {
      var form = this;
    
      e.preventDefault(); // <--- prevent form from submitting
    
      swal({
          title: "Are you sure?",
          text: "You will not be able to recover this imaginary file!",
          icon: "warning",
          buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
          ],
          dangerMode: true,
        }).then(function(isConfirm) {
          if (isConfirm) {
            swal({
              title: 'Shortlisted!',
              text: 'Candidates are successfully shortlisted!',
              icon: 'success'
            }).then(function() {
              form.submit(); // <--- submit form programmatically
            });
          } else {
            swal("Cancelled", "Your imaginary file is safe :)", "error");
          }
        })
    });
    

    UPD. Example above uses sweetalert v2.x promise API.

    Demo: http://plnkr.co/edit/YTY7PDs5Uh1XGUo9ic1s?p=preview

    0 讨论(0)
  • 2020-12-05 00:57

    You need To use then() function, like this

    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!"
     }).then(
           function () { /*Your Code Here*/ },
           function () { return false; });
    
    0 讨论(0)
提交回复
热议问题