How to confirm clicking on a link using jQuery

喜你入骨 提交于 2019-12-21 04:00:35

问题


I have many links in a HTML-table, which delete corresponding row, when clicked (calling a PHP-script via GET parameter).

They all have a class delete_row.

How could I please display a confirm('Really delete?') dialog using jQuery, when such a link is clicked?

And of course prevent following that link when No has been selected in the dialog.


回答1:


Try this.

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



回答2:


Very simple and effective one line solution without using jquery:

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



回答3:


you can use preventDefault method of the event object in the handler function:

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



回答4:


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>



回答5:


I think there is a error!

Use this:

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


来源:https://stackoverflow.com/questions/9044806/how-to-confirm-clicking-on-a-link-using-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!