2 回答

TA貢獻1951條經(jīng)驗 獲得超3個贊
首先,您需要將一個單擊事件偵聽器附加到您的繼續(xù)按鈕。在它的回調(diào)函數(shù)中,您可以檢查單選按鈕。
為此,您可以使用document.getElementsByName("selection"),它返回一個NodeList - 本質(zhì)上是一個數(shù)組 - 所有具有selection名稱的元素?,F(xiàn)在簡單地使用 for 循環(huán)遍歷數(shù)組,看看是否有一個.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條經(jīng)驗 獲得超2個贊
push()像這樣使用函數(shù):
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
如果您需要更多幫助,請詢問我。
添加回答
舉報