jquery-mobile create dynamic controlgroup and apply jquery-ui css [duplicate]

北慕城南 提交于 2019-12-05 04:57:03
$("#list").trigger('create');

From: jqm docs

if you generate new markup client-side or load in content via Ajax and inject it into a page, you can trigger the create event to handle the auto-initialization for all the plugins contained within the new markup. This can be triggered on any element (even the page div itself), saving you the task of manually initializing each plugin (listview button, select, etc.).

ervios

I do applogies if this post is too old and if my post isn't by the correct standard since it's the first time ever posting so please correct me if it's horribly bad :-]

But in case someone else comes across it, I had similar problems with how the dynamic data is displayed and I used the jsfiddles and comments above as a help, and this is what got mine to work, well somewhat near my solution, I don't have a button to load the data it's loaded automatically when the page is loaded.

Updated In my .html-file:

<div id="members"></div>     
<input type="button" id="load" value="test"/>

Updated In my .js-file:

$("#load").click(function(){

var name = ["NameOne","NameTwo", "NameThree"];
var fset = '<fieldset data-role="controlgroup" id="members-ctrlgroup"><legend>This is a legend:</legend>';
var labels='';

    for ( var i = 0; i < name.length; i++) {

        labels  += '<input type="checkbox" class="checkbox" id="c'
                + i
                + '"><label for="c'
                + i
                + '" data-iconpos="right">'
                + name[i]
                +'</label>';
    }
    $("#members").html(fset+labels+'</fieldset>');       
    $("#members").trigger("create");
});

I know the "field" looks a bit weird how I divided it but I find it somewhat easier when it comes to getting the whole html-string correct in these cases.

Updated In order to have the rounded corners and have it as one controlgroup you'll have to have this approach instead. Just like the former posters showed. Do note that the id with the checkbox and the label for= can tend to screw the output if they're not the same :-]

fiddle

In order to replace the content you should use .html(); instead of .append(), which adds the new content after the existing one. After adding content to a jQuery Mobile Page you need to enhance the content, using for instance $("input[type='radio']").checkboxradio();

I was using

for( var i=0 ; i < html.length ; i++ ){

                        var spline = html[i].split("|");

                        inHTML = inHTML +  " <input type=\"checkbox\" name=\"checkbox-"+i+"a\" id=\"checkbox-"+i+"a\" class=\"custom\" /> <label for=\"checkbox-"+i+"a\">"+ spline[0] +" , "+ spline[2] +"</label> ";


                    }

                    jq("fieldset#myFieldSet").empty();
                    jq("fieldset#myFieldSet" )
                    // Append the new rows to the body
                    .append( inHTML )
                    // Call the refresh method
                    .closest( "fieldset#myFieldSet" )

                    // Trigger if the new injected markup contain links or buttons that need to be enhanced
                    .trigger( "create" );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!