我在使用 jQuery 时遇到了一些问题。我想在用户单击链接删除某些内容时出现一个对话框,并提示他们确定他们确实要删除它。该对话框看起来很好,但是当用户单击"is"按钮时,我看不到获取链接 url 的方法。我尝试使用 event.relatedTarget
属性获取 a 标记的 url,但它为空。有谁知道如何做到这一点?
代码
<div id="dialog" title="Delete Run">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span>Are you sure you want to delete that run?</p>
</div>
$(document).ready(function() {
$('#dialog').dialog({
autoOpen: false,
width: 400,
modal: true,
draggable: false,
resizable: false,
buttons: {
"Yes": function(event) {
//Go to the url in $("a.delete")
},
"No": function() {
$(this).dialog("close");
}
}
});
$("a.delete").click(function(){
var url = $(this).attr("href");
$('#dialog').dialog('open');
return false;
});
});
最佳答案
<div id="dialog" title="Delete Run">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span>Are you sure you want to delete that run?</p>
</div>
$(document).ready(function() {
$('#dialog').dialog({
autoOpen: false,
width: 400,
modal: true,
draggable: false,
resizable: false,
buttons: {
"Yes": function(event) {
var url = $(this).data('url');
window.location = url;
},
"No": function() {
$(this).dialog("close");
}
}
});
$("a.delete").click(function(){
$('#dialog').data('url', $(this).attr("href"));
$('#dialog').dialog('open');
return false;
});
});
有点猜测,因为您的代码中没有元素 a.delete ? 使用 jQuery 的 data() 通常是比全局变量更好的选择。
关于javascript - JQuery 对话框绑定(bind)事件到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8378208/