【发布时间】:2017-01-26 02:06:47
【问题描述】:
作为练习,我正在创建一个不使用 jQuery 的简单测验应用程序。我正在尝试使用 javascript 添加单选按钮作为答案选项
html正文如下
<div class="row">
<div class="col-md-6 col-md-offset-3">
<p id="question"></p>
<form name="answers">
<fieldset class="form-group">
<div class="form-check" id="answersId">
</div>
</fieldset>
</form>
</div>
</div>
我下面的 js 脚本会在 div class="form-check" 中添加单选按钮
function firstQuestion() {
var answerSection = document.getElementById("answersId");
for(var i = 0; i < allQuestions[0].choices.length; i++) {
var radioElement = [];
radioElement[i] = document.createElement("input");
radioElement[i].setAttribute("class", "form-check-input");
radioElement[i].setAttribute("type", "radio");
radioElement[i].setAttribute("name", "choice");
radioElement[i].setAttribute("value", i);
var label = [];
label[i] = document.createElement("label");
label[i].setAttribute("class", "form-check-label");
var labelText = [];
labelText[i] = document.createTextNode(allQuestions[0].choices[i]);
label[i].appendChild(radioElement[i]);
label[i].appendChild(labelText[i]);
answerSection.appendChild(label[i]);
};
}
我无法获得引导程序外观,即:
-每个单选按钮都是内联的,而不是新行(垂直排列)
-按钮和标签之间的间距不像引导示例
为什么会这样?有人能指点我吗?
【问题讨论】:
标签: javascript twitter-bootstrap