jquery sweetAlert preventDefault并返回true

uqxowvwt  于 2023-06-22  发布在  jQuery
关注(0)|答案(4)|浏览(132)

我尝试了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,不起作用。之前我在onclick属性中使用了confirm()return truereturn false,它可以工作,但看起来很糟糕。

xmjla07d

xmjla07d1#

你可以试试这样的

$(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**

iqxoj9l9

iqxoj9l92#

我是这样做的:

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

kxeu7u2r3#

我是这样做的:

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

wydwbb8l4#

如果要保留事件的发生,可以使用带参数的触发器事件。这意味着您可以在表单或链接或基本上任何事件上使用它:

$(document).ready(function () {
  $('.link').click(function (ev, param) {
    if (param == 'approved') { // User approved
      return true; // Not preventing default because user approved
    }

    // stopping event, before might triggering the event again, depends on user confirming 
    ev.preventDefault();
    ev.stopPropagation();

    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
    }).then((result) => {
      if (result.isConfirmed) {
        // Triggering the event again, but with approve flag
        $(this).trigger('click', 'approved');
      }
    })
  })
});

相关问题