JQM (jQueryMobile) 动态添加的元素无法正确显示并且未应用 CSS

发布于 2024-10-21 02:36:07 字数 2223 浏览 4 评论 0原文

我在动态添加选择标签时遇到问题,CSS 和其他 html 标签(JQM 添加的)没有被应用。

以下是我如何添加新选择标签的示例: http://jsfiddle.net/UcrD8/4 /

HTML:

<div data-role="page" id="page_1">
    <div id="select_option_groups">
        <div data-role="fieldcontain">
            <select name="select_options_1">
                <option value="" selected>Please select an option</option>
                <option value="foo">Foo</option>
                <option value="bar">Bar</option>
            </select>
        </div>
    </div>
</div>

JS:

$(function(){
    var counter = 2;
    var onChange = function(e){
        val = $(':selected',this).val() || '';
        if (val != ''){
            // they may be able to toggle between valid options, too.
            // let's avoid that using a class we can apply
            var c = 'alreadyMadeASelection';
            if ($(this).hasClass(c))
                return;
            // now take the current select and clone it, then rename it
            var newSelect = $(this)
                .clone()
                .attr('name','select_options_'+counter)
                .attr('id','select_options_'+counter)
                .appendTo('#select_option_groups')
                .change(onChange);
            $(this).addClass(c);
            counter++;
        } else {
            var id = $(this).attr('name').match(/\d+$/), parent = $(this).parent();
            $(this).remove();

            // re-order the counter (we don't want missing numbers in the middle)
            $('select',parent).each(function(){
                var iid = $(this).attr('name').match(/\d+$/);
                if (iid>id)
                    $(this).attr('name','select_options_'+(iid-1));
            });
            counter--;
        }
    };
    $('#select_option_groups select').change(onChange);
});

我已经尝试过:

// this gets the select tags to stack but still no CSS
$(newSelect).fieldcontain('refresh'); 

// does nothing
$(newSelect).page();

// does nothing
$('#page_1').page();

I'm having an issue with adding a select tag dynamically, the CSS and additional html tags (that JQM add) are is not being applied.

Here is an example of how I'm adding the new select tag: http://jsfiddle.net/UcrD8/4/

The HTML:

<div data-role="page" id="page_1">
    <div id="select_option_groups">
        <div data-role="fieldcontain">
            <select name="select_options_1">
                <option value="" selected>Please select an option</option>
                <option value="foo">Foo</option>
                <option value="bar">Bar</option>
            </select>
        </div>
    </div>
</div>

The JS:

$(function(){
    var counter = 2;
    var onChange = function(e){
        val = $(':selected',this).val() || '';
        if (val != ''){
            // they may be able to toggle between valid options, too.
            // let's avoid that using a class we can apply
            var c = 'alreadyMadeASelection';
            if ($(this).hasClass(c))
                return;
            // now take the current select and clone it, then rename it
            var newSelect = $(this)
                .clone()
                .attr('name','select_options_'+counter)
                .attr('id','select_options_'+counter)
                .appendTo('#select_option_groups')
                .change(onChange);
            $(this).addClass(c);
            counter++;
        } else {
            var id = $(this).attr('name').match(/\d+$/), parent = $(this).parent();
            $(this).remove();

            // re-order the counter (we don't want missing numbers in the middle)
            $('select',parent).each(function(){
                var iid = $(this).attr('name').match(/\d+$/);
                if (iid>id)
                    $(this).attr('name','select_options_'+(iid-1));
            });
            counter--;
        }
    };
    $('#select_option_groups select').change(onChange);
});

I've tried:

// this gets the select tags to stack but still no CSS
$(newSelect).fieldcontain('refresh'); 

// does nothing
$(newSelect).page();

// does nothing
$('#page_1').page();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪恋 2024-10-28 02:36:07

尝试这个:

$(newSelect).selectmenu('refresh');

或者这个将强制重建它:

$(newSelect).selectmenu('refresh', true);

请让我知道它是否有效。

Try this:

$(newSelect).selectmenu('refresh');

or this which will force the rebuild of it:

$(newSelect).selectmenu('refresh', true);

and please let me know if it worked.

抱着落日 2024-10-28 02:36:07

我不知道为什么 .selectmenu('refresh'); 不起作用,但对于页面 - 您可以在元素上使用它一次。之后它下次会跳过该元素。

  1. 在添加内容之前克隆选择(不带参数的克隆)
  2. 删除原始
  3. 添加内容到克隆元素
  4. 并将其放回 dom 中
  5. 调用 .page().selectmenu()它,或者在包含它的元素上调用 .page()

应该有帮助。
如果没有,则尝试从头开始创建一个新的选择元素,并使用原始元素中的选项加载它并添加新的选项,然后继续。

[编辑]

以上只是猜测。你的代码就这样没问题。只需调用一次 .selectmenu()
工作代码:

http://jsfiddle.net/UcrD8/45/

I have no idea why .selectmenu('refresh'); doesn't work, but as for page - you can use it once on an element. After that it skips the element the next time.

  1. clone the select before adding stuff (clone without parameters)
  2. Remove the original
  3. add stuff to the cloned element
  4. put it back in dom
  5. call .page() or .selectmenu() on it, or call .page() on the element that contains it.

Should help.
If not, then try to create a new select element from scratch and load it with options from the original one and add new ones and then proceed.

[edit]

The above was just a guess. Your code is ok the way it is. just needs a single call to .selectmenu()
Working code:

http://jsfiddle.net/UcrD8/45/

守护在此方 2024-10-28 02:36:07

应该在要正确渲染的对象的父对象上使用 .trigger("create") 。

检查以下链接:

http://demos .jquerymobile.com/1.3.2/faq/injected-content-is-not-enhanced.html

以及以下答案:
https://stackoverflow.com/a/11054451/487812

Should use .trigger("create") on the parent of the object(s) you want to render correctly.

Check the following link:

http://demos.jquerymobile.com/1.3.2/faq/injected-content-is-not-enhanced.html

And the following answer:
https://stackoverflow.com/a/11054451/487812

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文