2016-11-23 102 views
0

我有一个表,其中每行有一个删除按钮以这种形式如何使用确认(然后提交)使用甜蜜警报?

<form id="tableDelete_1"> 
<input type="hidden" name="tipo" value="delete" /> 
<input type="submit" name="send" value="Delete" class="btn btn-danger btn-xs" onClick="return confirm(`Are you sure?`)" /> 
</form> 

<form id="tableDelete_2"> 
<input type="hidden" name="tipo" value="delete" /> 
<input type="submit" name="send" value="Delete" class="btn btn-danger btn-xs" onClick="return confirm(`Are you sure?`)" /> 
</form> 

那么这个页面的底部

$(document).on('submit', '[id^=tableDelete_]' , function() { 
return callAjax($(this).serialize()); 
}); 

function callAjax(data) { 
    $.ajax({ 
    type : 'POST', 
    url : 'call/page.php', 
    data : data, 
    success : function(data) { ... }, 
    error: function(data) { ... } 
    }); 
    return false; 
}; 

现在我想删除经典

​​

并使用sweetalert ... 我刚开始的时候有问题,我只想显示带有此功能的弹出框

$(document).on('submit', '[id^=tableDelete_]' , 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"); 
}); 
}; 

弹出只显示一秒钟,然后重新加载页面,因为我觉得在提交表单...

请给我帮助

回答

2

如果我阅读正确,我认为您正在寻找类似的东西?

$(document).on('submit', '[id^=tableDelete_]', function (e) { 
    e.preventDefault(); 

    var data = $(this).serialize(); 

    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!", 
    cancelButtonText: "No, cancel plx!", 
    closeOnConfirm: false, 
    closeOnCancel: false 
    }, 
    function (isConfirm) { 
     if (isConfirm) { 
     $.ajax({ 
      type: 'POST', 
      url: 'call/page.php', 
      data: data, 
      success: function (data) { 
      swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
      }, 
      error: function (data) { 
      swal("NOT Deleted!", "Something blew up.", "error"); 
      } 
     }); 
     } else { 
     swal("Cancelled", "Your imaginary file is safe :)", "error"); 
     } 
    }); 

    return false; 
}); 
+0

随着e.preventDefault()弹出窗口不显示...确定返回false! 并感谢其余的功能 – FireFoxII

0
$(document).on('submit', '[id^=tableDelete_]' , function(e) { 
e.preventDefault(); 
//do your popup stuff 
return false 
}); 

您需要防止默认当事件(通过e)发生时发生的事件。

+0

随着e.preventDefault()弹出不显示... Whitout这但将返回false我解决这个问题! – FireFoxII

+0

非常有趣 –

1

SweetAlert使用承诺确认回调:

swal({ 
     title: "Confirm?", 
     text: "Are you sure?", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Confirm", 
     cancelButtonText: "Back" 
     } 
    ).then(
     function (isConfirm) { 
     if (isConfirm) { 
      console.log('CONFIRMED'); 
     } 
     }, 
     function() { 
     console.log('BACK'); 
     } 
    ); 
相关问题