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


我正在使用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>

当我动态注入我的标记时 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");
});

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

我的现场测验演示 (抱歉, jsfiddle 没有列出jQuery Mobile)

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


10342
2018-01-19 07:18


起源

jsFiddle让你“管理资源”来添加jQuery Mobile。看到 jsfiddle.net/elijahmanor/5xwE8 左导航尝试添加此网址 code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.js 在你的jsFiddle - SarjanWebDev


答案:


添加此行

$("#quiz").trigger("create");

$("#questions").html(arr_quiz_markup.join("")).controlgroup("refresh");

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


12
2018-01-19 08:47





您将在此获得有关动态的更多信息 jQuery论坛帖子

使用以下代码后,将应用jQuery Mobile默认样式。这就是我发现的,它对我有用。

$("input[type='radio']").checkboxradio().checkboxradio("refresh"); 

3
2018-02-15 07:14





这对我有用(我正在使用jQuery Mobile 1.4.0):

HTML

<div id="locations" data-role="controlgroup"></div>

JS

$.ajax({
    ...
    success: function(data, textStatus, jqXHR) {
        // Hide loading message
        $.mobile.loading("hide");

        // Build page
        $("#location-page").trigger("create");

        // Clear another previos radio options
        $("#locations").controlgroup("container")["empty"]();

        // Read XML response
        var rows = $("Row", data);

        $(rows).each(function(index, value) {
            var locationId = $("LocationID", this).text();
            var locationName = $("LocationName", this).text();
            var $el = $("<label for=\"location" + index + "\">" + locationName + "</label><input type=\"radio\" name=\"location\" id=\"location" + index + "\" value=\"" + locationId + "\">")
            $("#locations").controlgroup("container")["append"]($el);
            $($el[1]).checkboxradio();
        });
        $("#locations").controlgroup("refresh");

        // Change to locations' page
        $.mobile.changePage("#location-page", {transition: "flip"});
    },
    ...
});

1
2018-02-11 18:51