我在页面上有几个链接,我想为每个链接显示单独的jQuery对话框。这是标记:
<html>
<body>
<div class="container">
<a href="#" class="delete_link">delete</a> <!-- when this link is clicked, dialog should popup -->
<div class="dialog_box"> <!-- this is the dialog for jquery UI -->
Pleasy specify a reson for your action
<textarea rows="5" cols="60"></textarea>
</div>
</div>
<div class="container">
<a href="#" class="delete_link">delete</a> <!-- when this link is clicked, dialog should popup -->
<div class="dialog_box"> <!-- this is the dialog for jquery UI -->
Pleasy specify a reson for your action
<textarea rows="5" cols="60"></textarea>
</div>
</div>
</body>
</html>
以下是JavaScript代码:
$(document).ready(function() {
$('.delete_link').click(function() {
alert('about to show jQuery dialog!');
var d = $(this).closest('DIV.container').find('DIV.dialog_box').dialog({
autoOpen: false,
title: 'You are going to delete a div!',
buttons: {
"Do delete": function() {
alert('You have entered:' + $(this).find('textarea').val());
$(this).dialog("close");
$(this).closest('DIV.container').hide(); //hiding div (primary action)
}
},
width: 800
});
d.dialog("open");
});
});
如您所见,触发事件的链接具有
delete_link
类,应该是jQuery UI对话框的DIV具有dialog_box
类。问题在于:当对话框打开并且用户按下“关闭”后,无法再次打开对话框。
根据谷歌和SO搜索,我不是第一个遇到这个问题的人。例如,这篇文章:http://blog.nemikor.com/2009/04/08/basic-usage-of-the-jquery-ui-dialog/。看来,对话框应该在
click()
动作之前进行某种初始化,但在所有解决方案中,页面上只有一个带有分配ID的对话框——我无法将其应用于我的情况。我尝试过这个,但它不起作用:
$(document).ready(function() {
//initializing
$('DIV.dialog_box').dialog({
autoOpen: false,
title: 'You are going to delete a div!',
buttons: {
"Do delete": function() {
alert('You have entered:' + $(this).find('textarea').val());
$(this).dialog("close");
$(this).closest('DIV.container').hide(); //hiding div (primary action)
}
},
width: 800
});
$('.delete_link').click(function() {
alert('about to show jQuery dialog!');
$(this).closest('DIV.container').find('DIV.dialog_box').dialog("open");
});
});
我已经在jsFiddle上准备了演示:http://jsfiddle.net/NQmhk/。没有使用jQUery UI css,但我希望这足以理解问题。非常感谢您的任何帮助。