如何使用 jQuery 确认点击链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9044806/
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
How to confirm clicking on a link using jQuery
提问by Alexander Farber
I have many links in a HTML-table, which delete corresponding row, when clicked (calling a PHP-script via GET parameter).
我在 HTML 表中有许多链接,单击时会删除相应的行(通过 GET 参数调用 PHP 脚本)。
They all have a class delete_row.
他们都有一个类delete_row。
How could I please display a confirm('Really delete?')dialog using jQuery, when such a link is clicked?
当单击这样的链接时,我如何使用 jQuery显示一个确认('真的删除?')对话框?
And of course prevent following that link when Nohas been selected in the dialog.
当然,当在对话框中选择“否”时,请防止跟随该链接。
回答by ShankarSangoli
Try this.
尝试这个。
$('.delete_row').click(function(){
return confirm("Are you sure you want to delete?");
})
回答by Indrasinh Bihola
Very simple and effective one linesolution without using jquery:
非常简单有效的单行解决方案,无需使用jquery:
<a href="to/your/path" onclick="return confirm('Are you sure you want to delete?');">Delete</a>
回答by Headshota
you can use preventDefault method of the event object in the handler function:
您可以在处理程序函数中使用事件对象的 preventDefault 方法:
jQuery('.delete_row').click(function(event){
if(!confirm('Really Delete?')){
event.preventDefault();
}
})
回答by seipsum
I think there is a error!
我认为有错误!
Use this:
用这个:
$('.delete_row').click(function(){
return confirm("Are you sure you want to delete?");
});
回答by Trace
You could also call another function after confirm like this:
您也可以在确认后调用另一个函数,如下所示:
<a href="to/your/path" onclick="return confirm('Are you sure you want to delete?') && yourOtherFunction();">Delete</a>