dqsvnsad79721 2016-03-29 08:44
浏览 78
已采纳

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

I have the code

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

and I have another code written in jquery and using sweetalert function which will popup a warning message if the user are trying to delete record in the database.

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

the problem of my code is, when the user click on the link, it will popup very fast, and go to the link given in href attribute. I would like to prevent it from going to the link, allowing the popup to stay display until the user decide whether to click the yes delete it or cancel and will not go to the link if the user click the cancel.

Any Help? I don't know what to do about it.

  • 写回答

2条回答 默认 最新

  • dongshi1424 2016-03-29 08:46
    关注

    You need to prevent the default action, and upon success, proceed with the location change:

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

    Also, it is always better not to have any kind of database write functions like:

    • Creating a new entry.
    • Deleting an entry.

    In the GET method. It is dangerous. So you should really think of changing it by giving a POST method and execute it through JavaScript / AJAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏