如何在 <a> 标签上暂停执行?

How to pause execution on <a> tag?

我有密码

<a href="<?php echo site_url('company/remove_company/'.$value['id']).'/'.$value['company_name']; ?>" title="Remove Data"><i class="icon-trash"></i></a>

我还有另一个代码用 jquery 编写并使用 sweetalert 函数,如果用户试图删除数据库中的记录,它将弹出一条警告消息。

$(".icon-trash").click(function(){
    swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false }, function() {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
      });
  });

我的代码的问题是,当用户点击 link 时,它会非常快速地弹出,然后转到 href 属性中给出的 link。我想阻止它转到 link,允许弹出窗口保持显示,直到用户决定是否单击 yes delete itcancel,并且不会转到 [=26] =] 如果用户单击 cancel.

有帮助吗?我不知道该怎么办。

您需要阻止默认操作,成功后,继续更改位置:

$(".icon-trash").click(function(e) {
    var that = this;
    // Prevent the default action.                                      « Look here.
    e.preventDefault();
    swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false }, function() {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
        // In this confirm, add the location.                           « Look here.
        location.href = $(that).attr("href");
      });
  });

此外,最好不要使用任何类型的数据库写入函数,例如:

  • 正在创建一个新条目。
  • 正在删除条目。

GET方法中。有危险。所以你真的应该考虑通过给一个 POST 方法来改变它并通过 JavaScript / AJAX.

执行它

您也可以通过以下方式实现:

link:

<a href="<?php echo site_url('company/remove_company/'. $value->id); ?>" class="delete-company">

脚本:

    $('.delete-company').on('click', function(e) {
    var that = $(this);
    swal({   
      title: "Are you sure?",   
      text: "You will not be able to recover this user account!",   
      type: "warning",   
      showCancelButton: true,   
      confirmButtonColor: "#DD6B55",   
      confirmButtonText: "Yes, delete it!",   
      cancelButtonText: "No, cancel please!",   
      closeOnConfirm: false,   
      closeOnCancel: false 
    }, 
    function(isConfirm){   
      if (isConfirm) { 
       location.replace(that.attr('href'));
       swal("Success","User successfully removed!", "success");
    } else {     
      swal("Cancelled", "Removing user accout was cancelled!", "error");   
    }
   });
    e.preventDefault();
  });