javascript 单击超链接后的确认框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4617039/
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-10-25 14:18:51  来源:igfitidea点击:

Confirm box after Clicking on Hyperlink

javascripthyperlink

提问by DogPooOnYourShoe

So I have got a hyperlink, which if pressed does a major action ( delete something from the database ) so I want a confirm box once it is pressed so that they dont make mistakes.

所以我有一个超链接,如果按下它会执行一个主要操作(从数据库中删除某些内容)所以我想要一个确认框,一旦按下它,他们就不会出错。

My code for the hyperlink is:

我的超链接代码是:

<a href='*****.php?Number3=1222&AssignedTo=331'>[x]</a>

I am unsure on Javascript, and I know this has a major part in it... Please help? PS The hyperlink's URL is random, and there are many of them so please dont make it so that it only works with one link

我不确定 Javascript,我知道这其中有很大一部分...请帮忙?PS 超链接的网址是随机的,而且有很多所以请不要弄成只对一个链接有效

回答by Anush Prem

try

尝试

<a href='*****.php?Number3=1222&AssignedTo=331' onclick="return confirm('Are you sure you want to delete?')" >[x]</a>

回答by benhowdle89

Give your href a class name and target it with something like jQuery/straight JS, then do this:

给你的 href 一个类名并用 jQuery/straight JS 之类的东西来定位它,然后执行以下操作:

var r=confirm("Are you sure you want to delete?");
if (r==true){
     return true;
{
else {
     return false;
}

回答by Rocket Hazmat

Add a class to the a tags that means you want a confirm box.

向 a 标签添加一个类,这意味着您需要一个确认框。

<a href="file.php" class="confirm">Link</a>

Then attach an event handler to those:

然后将事件处理程序附加到那些:

var links = document.getElementsByClassName('confirm');
for (var i = 0; i < links.length; i++) {
    links[i].onclick = function() {
        return confirm("Are you sure?");
    };
}

Or using jQuery:

或者使用 jQuery:

$('.confirm').live('click', function(){
    return confirm("Are you sure?");
});

回答by Vael Victus

Here's an answer for newer jQuery with some extra functionality for those that want custom confirm messages.

这是较新的 jQuery 的答案,为那些想要自定义确认消息的人提供一些额外的功能。

$('body').on('click', '[data-confirm]', function(){
    var msg = $(this).attr('data-confirm');
    return confirm(msg);
});

You use data-confirm instead of .confirm. Your HTML appears as follows:

您使用数据确认而不是 .confirm。您的 HTML 显示如下:

<span data-confirm="Are you sure?">delete</span>