javascript - jQuery UI 对话框重用行为

标签 javascript jquery jquery-ui-dialog

每次 onchange() 事件触发时,对话框都会递归地将自身嵌套在一个新层中。我怎样才能避免这种行为。不特定于版本。

http://jsfiddle.net/rqLqtuj5/2/

<input type=text onchange="popDialog(this)" value="test">
<div style="display:none">
    <p>dialog box</p>
<div>

function popDialog(obj) {
$('div')
    .dialog({
    buttons: {
        'Close': function () {
            $(this).dialog('close');
        }
    }
});}

最佳答案

嘿,我使用 empty() 命令清理 div 来解决这个问题,例如:

function popDialog(obj) {
    $('div').empty();
    $('div').dialog({
        buttons: {
            'Close': function () {
                $(this).dialog('close');
            }
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<p>Change the value in the text box and hit tab ...close the dialog and do it again, and again</p>
<input type=text onchange="popDialog(this)" value="test">
<div style="display:none">
    <p>dialog box</p>
<div>

这种方式更好:JSBIN

关于javascript - jQuery UI 对话框重用行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31325897/

相关文章:

javascript - 禁用按钮,除非从预先输入自动完成中选择

javascript - 切换对话框在 jQuery 中不起作用。如何在按钮点击时显示动画对话框?

javascript - 在 Enter 键上确认 Jquery UI 对话框提示

javascript - 找不到模块 : Error: Can't resolve './home.html' in '/Users/username/Documents/Privat/ionic3-barcodev3/src/app/home'

javascript - 在 <img> 上更改 'innerHTML' 不更改图像

javascript - 提交时将值加 1,直到达到 5

jquery - 更改 JQuery 对话框或 Google map Z 索引

javascript - 为什么我的 JavaScript 代码收到“"No ' Access-Control-Allow-Origin' header is present on the requested resources”错误,而 Postman 却没有?

javascript - Jquery 将所有文本更改为粗体而不是后续文本

jquery - 如何使用 jQuery 为文本对齐动态添加样式