您好,我如何使用queryselector从单选按钮中读取两个值并将值存储在javascript中的数组中。下面的代码示例是:使用querySelector从单选按钮读取2个值
<input type="radio" name="one" value="correct" class="firstRow"> Option 1 Mark 1: $650
<input type="radio" name="two" value="incorrect" class="secondRow"> Option 1 Mark 2: Twitter<br>
<input type="radio" name="one" value="incorrect" class="firstRow"> Option 2 Mark 1:$550
<input type="radio" name="two" value="incorrect" class="secondRow"> Option 2 Mark 2:Google<br>
<input type="radio" name="one" value="incorrect" class="firstRow"> Option 3 Mark 1:$650
<input type="radio" name="two" value="correct" class="secondRow"> Option 3 Mark 2:$650<br>
<!--button -->
<input type="button" value="Submit & Next Question" onclick="getAnswer3(this.form)" class="firstRow">
<input type="button" value="Cancel & Clear Selection" onclick="clearOptions(this.form)">
的Javascript我的想法:
function getAnswer3(form) {
var result = [];
//this only reads one value but i need to read 2 values..
var checked = form.querySelector("input[type=radio]:checked");
if(!checked) {
alert('Please select 2 answers..');
}
else{
//reads the values
}
//stores the values in the array..
}
而不是'input [type = one]:checked'应该是'input [type = radio]:checked' –
@The是更新了更正,但它只需要一个值而不是2个值,因为我打算多多谢谢 – user3497437