sweetAlert preventDefault并返回true

时间:2015-05-23 23:53:35

标签: jquery return preventdefault sweetalert

我尝试了sweeAlert插件,效果很好,但我无法确定如何在确认后执行默认操作。

$(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,但是没有用。 之前我在confirm()属性中使用了return true return falseonclick,但它看起来很糟糕。

3 个答案:

答案 0 :(得分:5)

您可以尝试这样的事情

$(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,output

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

标记看起来像这样

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

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

更新

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

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

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

答案 1 :(得分:1)

以下是我的表现:

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

答案 2 :(得分:0)

这是我的做法:

<script>

$(document).ready(function () {
    $("#submitDelete").on("click", function () {
        swal({
            title: "Delete important stuff?",
            text: "That seem like your deleting some important Item. Are you sure?",
            dangerMode: true,
            icon: "warning",
            buttons: true,
            dangerMode: true
        }).then((confirmed) => {
            if (confirmed) {
                $(function () {
                    $('#DeleteForm').submit(); // manully submit });
                });
            }
        });
    });
});