2012-01-28 41 views
9

我在HTML表中有很多链接,点击时会删除相应的行(通过GET参数调用PHP脚本)。如何确认点击使用jQuery的链接

他们都有一个类delete_row

如何才能请显示一个确认('真的删除?')对话框使用jQuery,点击这样的链接?

当然,在对话框中已经选择了没有,当然也阻止跟随该链接。

回答

33

试试这个。

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

是event.preventDefault()没有必要在这里? – 2012-01-28 11:17:28

+2

不需要,返回'true/false'会照顾它。 – ShankarSangoli 2012-01-28 11:19:14

+0

工作得很好,我怎样才能使用例如数据消息属性在对话框中定制消息? – petekaner 2016-01-31 09:53:12

2

可以使用在处理函数的事件对象的方法的preventDefault:

jQuery('.delete_row').click(function(event){ 
    if(!confirm('Really Delete?')){ 
     event.preventDefault(); 
    } 
}) 
0

我觉得有一个错误!

使用此:

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

非常简单而有效一行的解决方案,而无需使用jQuery的

<a href="to/your/path" onclick="return confirm('Are you sure you want to delete?');">Delete</a> 
+1

完美地工作;)谢谢 – 2017-12-21 06:17:59