sweetAlert preventDefault并返回true [英] sweetAlert preventDefault and return true

查看:119
本文介绍了sweetAlert preventDefault并返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 sweeAlert 插件,该插件效果很好,但是我不知道该怎么做默认值确认后.

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;
        }
    });
};
});

和按钮

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

我也尝试了unbind()off()而不是return false,但没有用. 之前我在onclick属性中将confirm()return truereturn false一起使用,它可以工作,但是看起来很糟糕.

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.

推荐答案

您可以尝试类似

$(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', {});
      }
    });
  }
};

这是一个演示 http://jsbin.com/likoza /1/edit?html,js,输出

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

另一种方法是使用表格而不是href.

Another way is the use a form instead of a href.

标记看起来像这样

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

,而不是window.location = $(this).attr('href');,您只能说form.submit()

如果页面上有多个元素,则可以像这样使用触发器

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

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

这是一个演示 http://output.jsbin.com/likoza/2

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

这篇关于sweetAlert preventDefault并返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆