【发布时间】:2011-07-10 08:54:00
【问题描述】:
我在页面上有几个链接,我想为每个链接显示单独的 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,但我希望它足以理解问题。
任何帮助将不胜感激。
【问题讨论】:
标签: jquery jquery-ui jquery-ui-dialog