SweetAlert preventDefault and returns true

I tried the sweeAlert plugin which works great, but I can't figure out how to make the default stuff after confirmation.

$(document).ready(function () {
function handleDelete(e){
    e.preventDefault();
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover the delaer again!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete!",
        closeOnConfirm: false
    },
    function (isConfirm) {
        if (isConfirm) {
            return true;
        }
    });
};
});

      

and the button

 <a href="{plink delete! $row->id_dealers}" class="delete" onclick"handleDelete(event);">&nbsp;</a>
 //{plink delete! $row->id_dealers} Nette -> calls php delete handler

      

I also tried unbind()

and off()

instead return false

, doesn't work. I used confirm()

to use with return true

and return false

in attribute onclick

, it works but looks terrible.

+3


source to share


2 answers


You can try something like this

$(document).ready(function () {
  $('.delete').on('click',function(e, data){
    if(!data){
      handleDelete(e, 1);
    }else{
      window.location = $(this).attr('href');
    }
  });
});
function handleDelete(e, stop){
  if(stop){
    e.preventDefault();
    swal({
      title: "Are you sure?",
      text: "You will not be able to recover the delaer again!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete!",
      closeOnConfirm: false
    },
    function (isConfirm) {
      if (isConfirm) {
        $('.delete').trigger('click', {});
      }
    });
  }
};

      

Here is a demo http://jsbin.com/likoza/1/edit?html,js,output

Another way is to use a form instead of href

.

The markup will look like this:

<form action="">
  <input type="submit" ...... />
</form>

      



and instead window.location = $(this).attr('href');

you can just sayform.submit()


Update

If there are multiple elements on the page, then you can use a trigger like this

$(e.target).trigger('click', {});

      

Here is a demo http://output.jsbin.com/likoza/2

+4


source


This is how I did it:



$('.delete').on('click', function(e) {
    e.preventDefault();
    var currentElement = $(this);

    swal({
            title: "Are you sure?",
            text: "You will not be able to recover the delaer again!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete!",
            closeOnConfirm: false
        },
        function () {
            window.location.href = currentElement.attr('href');
        }
    );
});

      

+1


source







All Articles