27

我有这样的链接。

<a href="delete.php?id=1" class="delete">Delete</a>

如果用户点击它。应弹出确认,然后仅当用户单击“是”时,它才应转到实际 URL。

我知道这可以防止默认行为

    function show_confirm()
    {
    var r=confirm("Are you sure you want to delete?");
    if (r==true)   {  **//what to do here?!!** }

    }    


    $('.delete').click(function(event) {
    event.preventDefault();
    show_confirm()
    });

但是,在确认后,我如何继续访问该链接或向该链接发送 ajax 帖子?

4

9 回答 9

44

您可以在点击中完成所有操作:

$('.delete').click(function(event) {
    event.preventDefault();
    var r=confirm("Are you sure you want to delete?");
    if (r==true)   {  
       window.location = $(this).attr('href');
    }

});

或者你可以通过将点击的元素传递给函数来做到这一点:

function show_confirm(obj){
    var r=confirm("Are you sure you want to delete?");
    if (r==true)  
       window.location = obj.attr('href');
}    
$('.delete').click(function(event) {
    event.preventDefault();
    show_confirm($(this));

});
于 2011-07-25T20:33:48.607 回答
3

我花了一段时间才弄清楚这一点,所以我想我会发布我的解决方案。

$('.delete').click(function(e){
    if(confirm('Are you sure?')){
        // The user pressed OK
        // Do nothing, the link will continue to be opened normally
    } else {
        // The user pressed Cancel, so prevent the link from opening
        e.preventDefault();
    }
}

我正在考虑确认错误的方式。确认将阻止站点自动打开,并等待用户输入。所以基本上,你需要将你的 preventDefault 移动到 else。

因此,只有当他们单击“取消”时,您才能阻止链接打开。这也允许链接正常运行,例如,如果它有一个 target="_blank" 指令。

于 2014-12-11T14:06:33.850 回答
2
function show_confirm(url){
    var r=confirm("Are you sure you want to delete?");
    if (r==true){
        location.top.href = url;
    }
}    


$('.delete').click(function(event) {
    event.preventDefault();
    show_confirm($(this).attr('href'));
});

如果你想使用ajax,你可以替换location.top.href = url;$.get(url);

于 2011-07-25T20:34:47.833 回答
2
function show_confirm(elem)
{
    var r=confirm("Are you sure you want to delete?");
    if (r==true) { 
        window.location.href = elem.href;
    }
}    

$('.delete').click(function(event) {
    event.preventDefault();
    show_confirm(this)
});
于 2011-07-25T20:35:27.917 回答
1

这是一个简短的形式:

$('.delete').click(function(){return confirm("Are you sure you want to delete?")});

我在网站上使用它来确认下载/链接。

于 2012-04-03T09:34:25.930 回答
1

要优化函数 show_confirm 中的代码,请尝试使用以下代码:

function show_confirm(obj){
   if(confirm("Are you sure you want to delete?")) window.location = obj.attr('href');
}
于 2012-11-07T10:15:21.140 回答
0

你可以做

function show_confirm()
    {
    if(confirm("Are you sure you want to delete?")){
        //make ajax call
     }else{

          //no ajax call
     }

    }    
于 2011-07-25T20:34:10.933 回答
0
 $('.delete').click(function() {

   if (confirm('Are you sure?')) {
     $.post($(this).attr('href'), function() {
       // Delete is OK, update the table or whatever has to be done after a succesfull delete
      ....
     }
   }

   return false;

}
于 2011-07-25T20:41:00.683 回答
0

如果您喜欢使用警报/确认,这是最好的方法(我更喜欢使用Bootstrap Confirmationbootbox):

$('.confirm-delete').click( function( event ) {
    if ( !confirm('Are you sure?') ) event.preventDefault();
});
于 2016-09-20T07:24:23.413 回答