使用jQuery动态创建垂直分组的单选按钮

Gre*_*reg 11 html javascript jquery-mobile

我正在使用jQuery mobile 1.0动态创建几个垂直分组的单选按钮,用于多项选择测验.

当我从JQM单选按钮文档中粘贴此代码时,它会正确地调整控件组的样式.

<fieldset data-role="controlgroup">
    <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
    <label for="radio-choice-1">Cat</label>
    <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2"  />
    <label for="radio-choice-2">Dog</label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

当我动态注入我的标记时,pageshow()会注入适当的标记,但它根本不会设置控件组的样式.

$.getJSON("assets/json/aircraft.json", function (data) {
    var maxQuestions = 10;
    var maxChoices = 4;

    var randomsorted = data.aircraft.sort(function (a, b) {
        return 0.5 - Math.random();
    });

    var quiz = $.grep(randomsorted, function (item, i) {
        return i < (maxQuestions * maxChoices);
    });

    var arr_quiz_markup = [];
    $.each(quiz, function (i, item) {
        var q = Math.floor(i / maxChoices);
        var c = i % maxChoices;

        if (c == 0) arr_quiz_markup.push("<div>Image for question " + q + " goes here...</div><fieldset data-role='controlgroup' data-question='" + q + "'>");

        arr_quiz_markup.push("<input type='radio' name='q" + q + "' id='q" + q + "c" + c + "' data-aircraftid='" + item.id + "' />");
        arr_quiz_markup.push("<label for='q" + q + "c" + c + "'>" + item.name + "</label>");

        if (c == maxChoices - 1 || c == quiz.length - 1) arr_quiz_markup.push("</fieldset><br />");

    });

    $("#questions").html(arr_quiz_markup.join("")).controlgroup("refresh");
});
Run Code Online (Sandbox Code Playgroud)

我试过$("#questions :radio").checkboxradio("refresh");但扔了"cannot call methods on checkboxradio prior to initialization; attempted to call method 'refresh'".

我的实时测验演示(对不起,jsfiddle没有列出jQuery Mobile)

我究竟做错了什么?如何刷新它以正确获取JQM以正确设置控件组的样式?

use*_*284 12

添加此行

$("#quiz").trigger("create");
Run Code Online (Sandbox Code Playgroud)

$("#questions").html(arr_quiz_markup.join("")).controlgroup("refresh");
Run Code Online (Sandbox Code Playgroud)

此代码段将强制重建测验页面,以便将jqm样式应用于页面内容.