Javasipt:操作radio标签详解
1、html代码:
<form action="#" name="userOpinionForm" id="userOpinionForm" method="post">
<input type="radio" name="opinionCategory" value="1" checked />最同意
<input type="radio" name="opinionCategory" value="0" />最不同意
</form>
2、js代码:
方法一:
var opinionCategory = document.getElementsByName('opinionCategory');
var opinionCategoryValue = 1;
for(var i=0;i<opinionCategory.length;i++){
if(opinionCategory[i].checked){
opinionCategoryValue = opinionCategory[i].value;
}
}
方法二:
var form = document.getElementById('userOpinionForm');
var num = 0;
for(var i=0;i<form.opinionCategory.length;i++){
if(form.opinionCategory[i].checked){
num++;
}
}