jQuery dynamic added select

风流意气都作罢 提交于 2020-04-21 04:36:16

问题


When I add a select dynamic to a DIV via clone, I'm not able to select anything from the new dropdown, any ideas why?

$(".inline-form .copyIng:first-child").clone().appendTo(".inline-form");

See this : http://jsfiddle.net/rxwL6/

.trigger("refresh"); Dosen't change anything, I still can't select anything from the new dropdown.

回答1:


Problem is that you are cloning html content that has already been 'enhanced' by jQM and not the original markup. Therefore jQM does not know how to create or refresh it.

Instead, if you know the markup ahead of time, just insert it like this:

$(document).on("pageinit", function () {
    $("#newIng").click(function () {
        var tocopy = $('<div class="copyIng"><div class="left"><input type="text" name="m" placeholder="Some text" /></div> <div class="ingDiv"><select size="1" name="c"><option value="">No 1</option><option value="">No 2</option><option value="">No 3</option></select></div></div>');       
        $(".inline-form").append(tocopy);
        $(".copyIng").trigger("create");
    });
});

here is your updated FIDDLE

UPDATE: From comment. OP is interested in cloning the list of options in the dropdown so they don't have to be retrieved from database each time. Here is an example of getting the option list and inserting just that into the new text beeing appended:

$(document).on("pageinit", function () {
    $("#newIng").click(function () {
        var optList = $(".ingDiv select").eq(0).html();                
        var tocopy = $('<div class="copyIng"><div class="left"><input type="text" name="m" placeholder="Some text" /></div> <div class="ingDiv"><select size="1" name="c">' + optList + '</select></div></div>');      
        $(".inline-form").append(tocopy);
        $(".copyIng").trigger("create");
    });
});

updated FIDDLE



来源:https://stackoverflow.com/questions/20474400/jquery-dynamic-added-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!