我有
<a href="test.php?id=2" class="confirm_delete">Click to proceed</a>
和下面的 JavaScript。当我单击上面的链接时,它会显示带有 2 个按钮的对话框。 “返回错误;”停止链接标签的默认事件。但我需要这样的功能,当我单击“是,删除”时,通过选择 onclicked anchor 的 href 值将我带到其他页面。我尝试过alert($(this).attr('id')); (因为我认为我可以使用“this.attr('href')”获取 HREF 值),但它显示“对话框”。
如何使其工作,以便当我单击类名为“confirm_delete”的链接时,它会显示对话框,如果我单击“取消”,它会保留在页面上,否则会根据 href 值将我带到页面。
$(".confirm_delete").click(function(){
$('<div id="dialog">Are you sure you want to delete?</div>').appendTo('body');
$("#dialog").dialog({
bgiframe: true, resizable: false, height:140, modal: true,
overlay: {
backgroundColor: '#000', opacity: 0.5
},
buttons: {
'Yes, Delete all items in recycle bin': function() {
$(this).dialog('close');
$(this).remove();
alert($(this).attr('id'));
},
Cancel: function() {
$(this).dialog('close');
$(this).remove();
}
}
});
return false;
});
谢谢。
最佳答案
首先:尽量不要在类名中使用下划线。我在某处读过它们可能会导致问题...
好吧,这里:
$('a.confirm-delete').click( function(event) {
if( !confirm('are you sure you want to go?') ) return false;
});
这里我使用了 javascript 确认对话框。您可以轻松地将其替换为 jquery 模式对话框。
jrh
关于javascript - 如何根据 Javascript 中的 "class"选择器检索 onclick 事件的 href 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/968874/