关于用 js 获取radio 和checkbox 的选中值问题:
1、 checkbox 可以获取到是否选中,是的话返回 true ,否则为 false。
2、 radio 只能循环进行获取选中值,有的话 返回value, 没有的话 返回 undefind
<input type="checkbox" name="ratio" id="check">
<input type="radio" name="lixi" value="1">
<input type="radio" name="lixi" value="2">
function check_form1() {
var radio = '';
var temp = document.getElementsByName("lixi");
for(var i=0;i<temp.length;i++)
{
if(temp[i].checked){
var radio = temp[i].value; // 这里获取选中的radio的value值
}
}
var check = document.getElementById('check').checked; // 这里返回 true 或 false
if(check){
var ratio = document.getElementById('ratio').value;
if(ratio < 0 || ratio > 100){
alert('比例不正确');
}else{
location.href = url+'&ratio='+ratio;
return false;
}
}else if(radio){
location.href = url+'&radio='+radio;
return false;
}else{
alert('请填写比例');
}
}