【问题标题】:jquery dialog widget does not preserve parentjquery 对话框小部件不保留父级
【发布时间】:2013-04-21 20:34:57
【问题描述】:

这是一个非常简单的脚本来说明我的观点。我想我只是做错了什么。 http://jsfiddle.net/9qx2q/2/

HTML

<form id='myForm'>
    <div class="panelClass"> 
        Here's my jquery panel
    </div>
</form>

对于脚本

//when the div is dialog'ed it's relocated to be a child of body?
$('.panelClass').dialog({
    //appendTo : '#myForm', // uncomment to see that this does not move the panel
    create : function(e, ui) {
        //$(this).dialog('widget').appendTo('#myForm'); //uncomment to see that this DOES move the panel
    }
});

输出

<body>
  <form id="myForm"></form>
  <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-

resizable" style="outline: 0px; z-index: 1003; position: absolute; height: auto; width: 300px; top: 235px; left: 104px; display: block;" tabindex="-1" role="dialog" aria-labelledby="ui-id-1"><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span id="ui-id-1" class="ui-dialog-title">&nbsp;</span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div><div class="panelClass ui-dialog-content ui-widget-content" style="width: auto; min-height: 82px; height: auto;" scrolltop="0" scrollleft="0"> 
            Here's my jquery panel
        </div><div class="ui-resizable-handle ui-resizable-n" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-e" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-w" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se ui-icon-grip-diagonal-se" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-sw" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-ne" style="z-index: 1000;"></div><div class="ui-resizable-handle ui-resizable-nw" style="z-index: 1000;"></div></div>
</body>

您将从上面的示例中看到,表单元素现在是空的,面板是在表单之后创建的。在面板上调用 .dialog() 时,面板会移动到&lt;body&gt; 的末尾。

基本上情况是这样的。 我在表单中有一个面板。该面板需要在表单内,因为当它弹出时,它将具有需要在表单上维护的输入元素。 (有时我们会弹出这些面板让用户提交更多信息等)。

虽然我确实有一个可行的解决方案,即通过更新 create 方法,但我真的不认为这应该首先移动元素......除非......我只是做错了什么。

任何建议将不胜感激。

谢谢

【问题讨论】:

    标签: javascript jquery html jquery-ui jquery-ui-dialog


    【解决方案1】:

    根据文档appendTo 将添加到 jQuery UI 1.10.0 http://api.jqueryui.com/dialog/#option-appendTo

    您所面临的是 jQuery UI 对话框的默认行为,它将对话框附加在正文 http://bugs.jqueryui.com/ticket/7948 的末尾,所以我认为这不是您的代码的问题。

    目前唯一的解决方法是您现在在 createopen 事件上做什么。

    编辑

    如果你愿意,也可以看看我的回答:Append jQuery UI dialog to its parent

    【讨论】:

    • 嘎!不敢相信我错过了文档中的评论。好吧,这就解释了为什么它对我不起作用。从那张票来看,他们最初似乎很不愿意添加这个。
    • @Beta033 添加了指向类似答案的链接以及可能的解决方法
    【解决方案2】:

    假设您使用的是 HTML5,您可以设置 form 属性以将 &lt;input&gt; 与表单相关联,而在 &lt;form&gt; 标记中没有元素:

    <input type="text" form="myForm">
    

    【讨论】:

    • 我想这可以在真空中工作,但随着系统周围面板的数量,单独或通过 jquery 选择器更改它是不切实际的。此外,破解我所有的图像按钮以绕过 jquery 无论如何都不应该做的事情感觉不对,即在页面上移动元素。
    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多