如何获取所选单选按钮的值?
2022-08-29 23:46:12
我的JS程序遇到了一些奇怪的问题。我让它正常工作,但由于某种原因它不再工作。我只想找到单选按钮的值(选择了哪个按钮)并将其返回到变量。由于某种原因,它不断返回。undefined
这是我的代码:
function findSelection(field) {
var test = 'document.theForm.' + field;
var sizes = test;
alert(sizes);
for (i=0; i < sizes.length; i++) {
if (sizes[i].checked==true) {
alert(sizes[i].value + ' you got a value');
return sizes[i].value;
}
}
}
submitForm
:
function submitForm() {
var genderS = findSelection("genderS");
alert(genderS);
}
网页:
<form action="#n" name="theForm">
<label for="gender">Gender: </label>
<input type="radio" name="genderS" value="1" checked> Male
<input type="radio" name="genderS" value="0" > Female<br><br>
<a href="javascript: submitForm()">Search</A>
</form>