[英]how to read if radio button checked and save it as 1 with javascript?
I have 3 radio buttons: 我有3个单选按钮:
<div id="step-1">
<h2 class="StepTitle">
<label style="font-weight: bold; color: #662819;" id="frage1"></label>
</h2>
<br />
<input type="radio" name="group1" id="antwort1" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
<label id="antwort1fuerFrage1"></label>
<br />
<br />
<input type="radio" name="group1" id="antwort2" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
<label id="antwort2fuerFrage1"></label>
<br />
<br />
<input type="radio" name="group1" id="antwort3" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
<label id="antwort3fuerFrage1"></label>
</div>
I am trying to read if radio button is checked, and save a value (need 1 or/ 0) like this: 我正在尝试读取是否选中了单选按钮,并像这样保存一个值(需要1或/ 0):
var frage1 = document.getElementById("frage1").innerHTML;
var antwort1 = document.getElementById("antwort1");
var antwort2 = document.getElementById("antwort2");
var antwort3 = document.getElementById("antwort3");
antwort1 = $('input[id="antwort1"]:checked').val();
antwort2 = $('input[id="antwort2"]:checked').val();
antwort3 = $('input[id="antwort3"]:checked').val();
antwort 1, 2, 3 are undefined. 未定义1、2、3。 I need to get
0
or 1
. 我需要得到
0
或1
。
You could just use .length
since you're querying IDs: 您可以只使用
.length
因为您要查询ID:
antwort1 = $('#antwort1:checked').length;
antwort2 = $('#antwort2:checked').length;
antwort3 = $('#antwort3:checked').length;
You could do like this: 您可以这样:
var antwort1 = $('#antwort1:checked').length;
var antwort2 = $('#antwort2:checked').length;
console.log(antwort1);
console.log(antwort2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.