我有这个代码>>
function myFunction() {
var a = document.querySelector('input[name="qa1"]:checked').value;
var b = document.querySelector('input[name="qa2"]:checked').value;
var sum = a + b;
alert(sum)
}
<label><input name="q1" value="5" required="" type="radio"> answare</label>
<label><input name="q2" value="10" required="" type="radio"> answare2</label>
<input class="submit-button" value="Score my Answers" name="submit" type="submit" onclick="myFunction()">
我希望在点击“分数我的答案”按钮并选中时,获得q1和q2之和的警报 请帮忙
答案 0 :(得分:0)
名字上有拼写错误。其次,值是一个字符串,因此字符串连接的输出将为510
。在添加
parseInt
将字符串转换为num
function myFunction() {
var a = document.querySelector('input[name="q1"]:checked').value;
var b = document.querySelector('input[name="q2"]:checked').value;
var sum = parseInt(a, 10) + parseInt(b, 10);
alert(sum)
}
<label><input name="q1" value="5" required="" type="radio"> answare</label>
<label><input name="q2" value="10" required="" type="radio"> answare2</label>
<input class="submit-button" value="Score my Answers" name="submit" type="submit" onclick="myFunction()">