2 回答
TA贡献1951条经验 获得超3个赞
首先,您需要将一个单击事件侦听器附加到您的继续按钮。在它的回调函数中,您可以检查单选按钮。
为此,您可以使用document.getElementsByName("selection"),它返回一个NodeList - 本质上是一个数组 - 所有具有selection名称的元素。现在简单地使用 for 循环遍历数组,看看是否有一个.checked属性为 true的元素并采取相应的行动。
下面是一个例子:
function check() {
var found=false;
var inputElements = document.getElementsByName("selection");
for (var a = 0; a < inputElements.length; a++) {
if (inputElements[a].checked) {
console.log(inputElements[a].value + " is checked!");
found=true;
// continue!
}
}
if(!found)
{
console.log("nothing selected!");
}
}
document.getElementsByClassName("r7-button")[0].addEventListener("click", check);
<!-- begin radio buttons -->
<div class="panel-variation">
<div class="panel-body"></div>
<div class="radio-buttons-variation">
<ul class="radio-buttons-left">
<li>
<input type="radio" name="selection" id="A" value="Hanna">
<label>Hanna</label>
</li>
<li>
<input type="radio" name="selection" id="B" value="Bryan">
<label>Bryan</label>
</li>
<li>
<input type="radio" name="selection" id="C" value="Alex">
<label>Alex</label>
</li>
</ul>
<ul class="radio-buttons-right">
<li>
<input type="radio" name="selection" id="D" value="Gabby">
<label>Gabby</label>
</li>
<li>
<input type="radio" name="selection" id="E" value="Dan">
<label>Dan</label>
</li>
<li>
<input type="radio" name="selection" id="F" value="Ryan">
<label>Ryan</label>
</li>
</ul>
</div>
</div>
<!-- end radio buttons -->
<!-- begin continue button -->
<div class="buttons">
<a href="#" class="r7-button">continue</a>
</div>
TA贡献2011条经验 获得超2个赞
push()像这样使用函数:
var answers = [];
// getting the selected radio button value
function select () {
var answer = document.querySelector('input[name="selection"]:checked').value;
answers.push(answer); //transferring value to continue button
}
// Index marks which question it was so answers[0] would be the first question and
answers[1] would be the second
如果您需要更多帮助,请询问我。
添加回答
举报