sweetAlert preventDefault and return true

﹥>﹥吖頭↗ 提交于 2019-12-07 11:52:11

问题


I tried sweeAlert plugin, which works perfectly, but I cant figure out how to do default stuff after confirm.

$(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 of return false, doesnt work. Earlier I used confirm() with return true and return falsein onclick attribute, it works, but it looks awful.


回答1:


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 the use a form instead of a href.

The markup would look like this

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

and instead of window.location = $(this).attr('href'); you can just say form.submit()


Update

If there are multiple elements on the page then trigger can be used like this

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

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




回答2:


Here's 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');
        }
    );
});


来源:https://stackoverflow.com/questions/30418718/sweetalert-preventdefault-and-return-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!