防止链接,确认然后转到位置 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6821968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:25:29  来源:igfitidea点击:

Prevent a link, confirm and then goto location jquery

jquerypopupconfirmationpreventdefault

提问by esafwan

I have links like this.

我有这样的链接。

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

If a user click on it. A confirmation should popup and then only if user click yes, it should goto the actual url.

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

I know that this can prevent the default behavior

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

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

But how do i continue to that link or send an ajax post to that link after confirming?

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

回答by locrizak

you could do it all within the click:

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

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

});

Or you could do it by passing the clicked element to the function:

或者您可以通过将单击的元素传递给函数来实现:

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));

});

回答by Travis Heeter

It took me a while to figure this out, so I figured I'd post my solution.

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

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

I was thinking about confirm the wrong way. Confirm will prevent the site from opening automatically, and wait for the user's input. So basically, you need to move your preventDefault to the else.

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

So you only prevent the link from opening if they click Cancel. This also allows for the link to function as it normally would, for instance if it has a target="_blank" instruction.

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

回答by Paul

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'));
});

If you want to use ajax you can replace location.top.href = url;with $.get(url);

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

回答by ChristopheCVB

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)
});

回答by Matteo Conta

This is a short form:

这是一个简短的形式:

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

I use it on web sites for download/link confirmation.

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

回答by Chanraksmey

To optimize code in function show_confirm, try to use below :

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

function show_confirm(obj){
   if(confirm("Are you sure you want to delete?")) window.location = obj.attr('href');
}

回答by Roy Shoa

If you like to use alert/confirm, this is the best way (I prefer to use Bootstrap Confirmationor bootbox):

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

$('.confirm-delete').click( function( event ) {
    if ( !confirm('Are you sure?') ) event.preventDefault();
});

回答by Claudio

 $('.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;

}

回答by Nicola Peluchetti

you could do

你可以

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

          //no ajax call
     }

    }