简体   繁体   English

动态添加单选按钮

[英]Add radio buttons dynamically

i am having some serious issue with jquery mobile am build a learning reponse system.Now i want to dynamically populate the question page each time there is a question to be answered. 我在使用jquery mobile时遇到一些严重问题,正在构建一个学习响应系统。现在,我想在每次有问题要回答时动态填充问题页面。 so far i have been able to do all that but currently my radio button are refusing to follow the normal jquery order when i call them dynamically. 到目前为止,我已经能够执行所有操作,但是当前我的单选按钮在我动态调用它们时拒绝遵循正常的jquery顺序。 When i call the dynamically the design disrupts but when i hard code it i get exactly what am expecting. 当我动态调用时,设计就中断了,但是当我进行硬编码时,我得到的正是预期的结果。 the image below is an example of wat am trying to explain. 下图是wat试图解释的示例。 the upper distorted radio buttons are the dynamic ones the lower 2 are the hard coded ones. 上方失真的单选按钮是动态按钮,下方2是硬编码的按钮。

this is the code i used: 这是我使用的代码:

var possibleAnswers=poss_ans.split(",");//poss_ans is string containing all the answer from the database


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

                var label = sp[i];
                   $radio = $('<input />', { type: "radio" }); 
                   var $label = $('<label />', { text: label});
                   var wrapper = $('<div />');
                   wrapper.append($label).append($radio);;

                    $('div#poss_ans').append(wrapper);                      
            }

Please this is my html am sorry but am not realy good with the fiddle thats why i didnt post there. 请这是我的html,对不起,但不是很好,这就是为什么我没有张贴在那里。

    <fieldset class="ui-grid-b">
            <h1 id="title">H1 Heading</h1>
            <p id="question">The question</p>


            <div data-role="fieldcontain">
            <fieldset  data-role="controlgroup">
                <div id="poss_ans"></div>


                    <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2"  />
                    <label for="radio-choice-2">6</label>

                    <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3"  />
                    <label for="radio-choice-3">4</label>
                    <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3"  />
                    <label for="radio-choice-3">8</label><input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3"  />
                    <label for="radio-choice-3">2</label>


            </fieldset>
        </div>
        </div>

Any help or contribution at all would grateful.Thanks 任何帮助或贡献都将不胜感激。 动态和硬编码

After testing your code and putting in the default string of values and also adding in the value to the dynamically created radio I can confirm it's working fine. 测试完代码并放入默认值字符串,并将该值添加到动态创建的无线电之后,我可以确认它运行正常。 I would imagine your issue is that there is problem with the main part of your HTML. 我想您的问题是HTML的主要部分存在问题。 But as you haven't added it, i can't confirm it. 但由于您尚未添加,因此无法确认。

Here's your javascript code: 这是您的JavaScript代码:

var poss_ans = "6,4,8,2";
var possibleAnswers = poss_ans.split(","); //poss_ans is string containing all the answer from the database

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

    var label = possibleAnswers[i];
    $radio = $('<input />', {
        type: "radio",
        value: possibleAnswers[i],
    });
    var $label = $('<label />', {
        text: label
    });
    var wrapper = $('<div />');
    wrapper.append($label).append($radio);

    $('div#poss_ans').append(wrapper);
}

And the HTML is used: 并使用HTML:

<div id="poss_ans"></div>

And finally, a working Fiddle - http://jsfiddle.net/andyjh07/tw0k5qkc/ 最后,一个有效的小提琴-http: //jsfiddle.net/andyjh07/tw0k5qkc/

For jQuery Mobile, after adding controls dynamically you need to initialize the widgets or call refresh on them so they apply the jQM classes. 对于jQuery Mobile,在动态添加控件之后,您需要初始化小部件或对其调用刷新,以便它们应用jQM类。 For your case, you can simplify the HTML: 对于您的情况,可以简化HTML:

<h1 id="title">H1 Heading</h1>
<p id="question">The question</p>
<fieldset  data-role="controlgroup">
    <div id="poss_ans" ></div>
</fieldset>

Then in the script. 然后在脚本中。 you need to add a name to the radio buttons so that checking one unchecks the others. 您需要在单选按钮上添加一个名称,以便选中一个取消选中其他按钮。 Also add an id and in the label a for that points at the associated id. 还要添加一个ID,并在标签a中指向相关ID。 Finally, after all appending is done, call enhanceWithin() on the div to initialize the checkboxradio widgets and then tell the controlgroup fieldset to refresh itself. 最后,在完成所有附加操作之后,在div上调用EnhanceWithin()来初始化checkboxradio小部件,然后告诉控件组字段集刷新自身。

var $posans = $('#poss_ans');    
for (var i = 0; i < possibleAnswers.length; i++) {
    var label = possibleAnswers[i];
    $radio = $('<input />', {
        id: 'rad' + i,
        name: 'possibleAnswers',
        type: "radio",
        value: possibleAnswers[i],
    });
    var $label = $('<label />', {
        for: 'rad' + i,
        text: label
    });

    $posans.append($label).append($radio);
}     
$posans.enhanceWithin().closest("fieldset").controlgroup("refresh");

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM