简体   繁体   English

关闭动态创建的jQuery-ui对话框

[英]Closing a dynamically created jQuery-ui dialog

I am creating a user information edit dialog that fetches edit-user information using $.post but I'm unable to close this dialog since dialog wasn't initialized using any HTML element. 我正在创建一个用户信息编辑对话框,该对话框使用$.post获取编辑用户信息,但是由于没有使用任何HTML元素初始化对话框,因此无法关闭此对话框。

I am trying $('#editUser').dialog('close') but it won't work. 我正在尝试$('#editUser').dialog('close')但是它不起作用。

Here is the main body: 这是主体:

<div id='userInfo'>
<div class='user'>
    <span class='userId'>1</span>
    <span class='userName'>John</span>
</div>
<div class='user'>
    <span class='userId'>2</span>
    <span class='userName'>Jane</span>
</div>

and here is the script used to create the dialog: 这是用于创建对话框的脚本:

$(function() {
$('.user').click(function() {
     var uid = $(this).find('span.userId').html();
    $post('/e-val/user/edit.php', {id: uid}, function(html) {
       $(html).dialog();
    });
});

$('body').on('click', '#closeEditDialog', function() {
   $('#editUser').dialog('close')
});
});

The dialog opens up fine as expected but isn't closing as it should. 对话框可以按预期正常打开,但没有按预期关闭。

This is the HTML for the dialog as returned by the ajax script. 这是ajax脚本返回的对话框的HTML。

<div id='editUser'>
<table>
    <tr>
        <td>Username</td>
        <td><?php echo $user['name'] ?></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><?php echo $user['email'] ?></td>
    </tr>
    <tr>
        <td colspan='2'>
<input type='button' id='closeEditDialog' value='close' />
</td>
    </tr>
</table>
</div>

What can I do to close it? 我该怎么做才能关闭它? I can use $('#editUser').remove() to remove the dialog, but I need to close it not remove it. 我可以使用$('#editUser').remove()删除对话框,但是我需要关闭它而不是删除它。

var mydialog;
$(function() {
$('.user').click(function() {
     var uid = $(this).find('span.userId').html();
    $post('/e-val/user/edit.php', {id: uid}, function(html) {
       mydialog = $(html);
       mydialog.appendTo('body');
       mydialog.dialog();
    });
});

$('body').on('click', '#closeEditDialog', function() {
   mydialog.dialog('close')
});
});

You might need to insert that html into your DOM before creating your dialog. 在创建对话框之前,您可能需要将该html插入到DOM中。

$("body").append(html);
$("#editUser").dialog();

Well at least if your dialog shows up this way, there is nothing preventing it from closing, you're using the same selector. 好吧,至少如果您的对话框以这种方式显示,没有什么阻止它关闭的,您使用的是相同的选择器。

EDIT 编辑

Also, do not forget that .dialog() will initialize the widget, try not calling it more than once. 另外,不要忘记.dialog()将初始化小部件,请尝试不要多次调用它。 Using .dialog("open") instead. 改用.dialog(“ open”)。

Best would be even to already add the dialog's div into your html, and then append your server side code in it to dynamically update the content of the dialog. 最好甚至将已经将对话框的div添加到您的html中,然后在其中附加服务器端代码以动态更新对话框的内容。

$('#editUser').dialog('close') won't work because you've never used $('#editUser') to initialize the dialog , so you cannot use it either to close it, You need to use the same handler that was used to create it. $('#editUser').dialog('close')无法使用,因为您从未使用$('#editUser')初始化dialog ,因此您不能使用它来关闭它,您需要使用用于创建它的同一处理程序。

As answered here by Gil & Trinh : Just add the dialog content to the DOM first and then initialize the dialog: 正如Gil&Trinh回答的那样:只需先将对话框内容添加到DOM中,然后初始化对话框即可:

$post('/e-val/user/edit.php', {id: uid}, function(html) {
   $(html).appendTo('body');
   $('#editUser').dialog( {autoOpen: false} );
});

autoOpen: false will prevent the dialog from opening by itself and it can be opened using $('#editUser').dialog('open') anytime. autoOpen: false将阻止对话框自行打开,可以随时使用$('#editUser').dialog('open')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM