2012-01-28 39 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