Jquery,从点击获取url并传递给对话框中的确认按钮

我有这个jquery代码:

$("#deletec-box").dialog({ autoOpen: false, resizable: false, height:230, modal: true, buttons: { "Confirm": function() { window.location = 'hrefurlfromclick'; $(this).dialog("close"); }, Cancel: function() { $(this).dialog("close"); } } }); $("#deletec-confirm").click(function() { $("#deletec-box").dialog("open"); return false; }); 

然后在页面中我有一个调用对话框的链接:

 Delete 

我的问题是如何获得href的值,如果该人确认它加载了他们最初点击的链接?

您可以使用以下命令获取href属性: $('#deletec-confirm').attr('href');

您的代码现在看起来像:

 $("#deletec-box").dialog({ autoOpen: false, resizable: false, height:230, modal: true, buttons: { "Confirm": function() { window.location = $('#deletec-confirm').attr('href'); $(this).dialog("close"); }, Cancel: function() { $(this).dialog("close"); } } }); $("#deletec-confirm").click(function() { $("#deletec-box").dialog("open"); return false; }); 

你必须使用jQuery UI对话框吗? 您可以改为使用确认框,如下所示:

 $("#deletec-confirm").click(function(event) { if(!confirm("Are you sure you want to delete this item?")) { event.preventDefault(); } }); 

这样,如果点击取消按钮,则链接原始动作被阻止,否则链接可以自由地执行它的原始动作。

这是一个简单的例子。

通过attr获取它

 var url = $('#deletec-confirm').attr('href'); $("#deletec-box").dialog({ autoOpen: false, resizable: false, height:230, modal: true, buttons: { "Confirm": function() { window.location = url; $(this).dialog("close"); }, Cancel: function() { $(this).dialog("close"); } } });