javascript - 带确认的 Jquery 模式在用户确认之前提交

标签 javascript jquery ajax

我让用户使用 jquery 和 ajax 删除功能。当用户单击删除时,会出现弹出窗口,并使用按钮询问他是否确定。问题在于,在单击Delete 的瞬间执行了脚本并删除了用户。用户是否点击也没关系。

我使用这个jquery确认插件: https://myclabs.github.io/jquery.confirm/

这是jquery部分

$(document).ready(function() {

    $('.delete').click(function() {
      var parent = $(this).closest('.header-profile');
      $.ajax({
         type: 'get',
         url: 'misc/friendRemove.php', 
         data: 'ajax=1&delete=' + $(this).attr('id'),
         beforeSend: function() {
            parent.animate({'backgroundColor':'#fb6c6c'},300);
         },

             success: function()
             {
                parent.fadeOut('slow', function() {$(this).remove();});
             }
      });                 
    });

    $('.delete').confirm({
       text: "Are you sure?",
       title: "Please confirm",

       confirmButton: "Yes",
       cancelButton: "No",
       post: true,
       confirmButtonClass: "btn-danger",
       cancelButtonClass: "btn-default",
       dialogClass: "modal-dialog modal-lg"
    });      
});

还有按钮

<a href="" class="delete" id="'.$row['id'].'"><i class="fa fa-times pull-right"></i></a>

最佳答案

您必须在confirm方法上调用ajax删除方法。下面是代码:

function deleteItem(parent, id){
            $.ajax({
             type: 'get',
             url: 'misc/friendRemove.php', 
             data: 'ajax=1&delete=' + id,
             beforeSend: function() {
                parent.animate({'backgroundColor':'#fb6c6c'},300);
             },

             success: function() {
                 parent.fadeOut('slow', function( {$(this).remove();});
             }
          });         
        }

    $(document).ready(function() {
        var parent, id;
        $('.delete').click(function(event) {
          parent = $(this).closest('.header-profile');
          id = $(this).attr('id');
          event.preventDefault();        
        });


        $('.delete').confirm({
           text: "Are you sure?",
           title: "Please confirm",
           confirmButton: "Yes",
           cancelButton: "No",
           post: true,
           confirmButtonClass: "btn-danger",
           cancelButtonClass: "btn-default",
           dialogClass: "modal-dialog modal-lg",
            confirm: function(button) {
            deleteItem(parent, id);
            }
        });      
    });

关于javascript - 带确认的 Jquery 模式在用户确认之前提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33435623/

相关文章:

javascript - Laravel , Jquery 通过选择的下拉列表填充数据

javascript - 仅在 IE7 中禁用 JavaScript

jquery多个Slidetoggle导致页面抖动

jquery - 在 Rails 3 中通过 .ajax 渲染静态部分

jquery - 在 ajax post 之前添加数据到表单

javascript - 将函数参数重新声明为变量?

javascript - 覆盖所有目标 ="_blank"以在同一页面上打开链接的最佳方法

javascript/jquery 克隆在下拉列表中不起作用

javascript - 加载时隐藏元素,使用 Mouseenter 悬停时显示

php - AJAX:每分钟显示一条随机线