javascript如何判断复选框是否全部被选中 本帖最后由 a124699279 于 2014-05-20 14:53:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 function dan() { var a = document.getElementsByName("danxuan"); for(i=0;i<a.length;i++) { if(a[i].type!="checkbox") { document.getElementByName("duoxuan").checked=false; } } } <script>window.onload=function(){ var oBtn=document.getElementById('btn'); oBtn.onclick=function(){ if(detechIsAllChecked('group_name')) alert('全部选中'); else alert('不是全部选中'); }}function detechIsAllChecked(name){ var aEles=document.getElementsByName(name); for(var i=0;i<aEles.length;i++) { if(!aEles[i].checked) return false; } return true;}</script></head><body><p><input type="checkbox" value="0" name="group_name" id="group_name_1"><label for="group_name_1">选项1</label><input type="checkbox" value="1" name="group_name" id="group_name_2"><label for="group_name_2">选项2</label><input type="checkbox" value="2" name="group_name" id="group_name_3"><label for="group_name_3">选项3</label><input type="checkbox" value="3" name="group_name" id="group_name_4"><label for="group_name_4">选项4</label></p><input type="button" id="btn" value="检测"/></body> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Insert title here</title><script>function selectRadio(a){//alert(a==1); if(a==1) loopCheckbox(true); if(a==2) loopCheckbox(false); }//循环复选框function loopCheckbox(param){//alert(param==true); var checkboxs = document.getElementsByName("checkbox"); for(var i=0;i<checkboxs.length;i++) { if(param == true){ checkboxs[i].checked = true;} else checkboxs[i].checked = false; }}//判断复选框function checkCheckbox(){ var checkboxs = document.getElementsByName("checkbox"); for(var i=0;i<checkboxs.length;i++) { if(checkboxs[i].checked == false) return false; } return true;}function selectCheckbox(){ var radios = document.getElementsByName("radio"); for(var i=0;i<radios.length;i++) { if(checkCheckbox()) { if(radios[i].value == "全选") radios[i].checked = true; }else { if(radios[i].value == "不全选") radios[i].checked = true; } }}</script></head><body><input type="radio" name="radio" onclick="selectRadio(1)" value="全选"/>全选<br/><input type="radio" name="radio" onclick="selectRadio(2)" value="不全选" />不全选<br/><input type="checkbox" name="checkbox" onclick="selectCheckbox()">1<input type="checkbox" name="checkbox" onclick="selectCheckbox()">2</body></html> 像百度一样,一段脚本在页面中不执行 怎么实现的 js innerHTML输出问题 jQuery制作左侧下拉菜单的问题! JQUERY js获取select 显示内容 请教单选框的值变化问题 求树一棵 怎么通过option的value 获得这个value在下拉列表中的索引 JavaScript的方法局部变量也可以持有状态? 窗口居中? 求助 js undefined is not a function 在线等大神啊 这几个字到底藏在哪了
{
var a = document.getElementsByName("danxuan");
for(i=0;i<a.length;i++)
{
if(a[i].type!="checkbox")
{
document.getElementByName("duoxuan").checked=false;
}
}
}
window.onload=function()
{
var oBtn=document.getElementById('btn');
oBtn.onclick=function(){
if(detechIsAllChecked('group_name'))
alert('全部选中');
else
alert('不是全部选中');
}
}
function detechIsAllChecked(name)
{
var aEles=document.getElementsByName(name);
for(var i=0;i<aEles.length;i++)
{
if(!aEles[i].checked)
return false;
}
return true;
}
</script>
</head><body>
<p>
<input type="checkbox" value="0" name="group_name" id="group_name_1"><label for="group_name_1">选项1</label>
<input type="checkbox" value="1" name="group_name" id="group_name_2"><label for="group_name_2">选项2</label>
<input type="checkbox" value="2" name="group_name" id="group_name_3"><label for="group_name_3">选项3</label>
<input type="checkbox" value="3" name="group_name" id="group_name_4"><label for="group_name_4">选项4</label>
</p>
<input type="button" id="btn" value="检测"/>
</body>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
<script>
function selectRadio(a)
{//alert(a==1);
if(a==1)
loopCheckbox(true);
if(a==2)
loopCheckbox(false);
}
//循环复选框
function loopCheckbox(param)
{//alert(param==true);
var checkboxs = document.getElementsByName("checkbox");
for(var i=0;i<checkboxs.length;i++)
{
if(param == true){
checkboxs[i].checked = true;}
else
checkboxs[i].checked = false;
}
}
//判断复选框
function checkCheckbox()
{
var checkboxs = document.getElementsByName("checkbox");
for(var i=0;i<checkboxs.length;i++)
{
if(checkboxs[i].checked == false)
return false;
}
return true;
}
function selectCheckbox()
{
var radios = document.getElementsByName("radio");
for(var i=0;i<radios.length;i++)
{
if(checkCheckbox())
{
if(radios[i].value == "全选")
radios[i].checked = true;
}else
{
if(radios[i].value == "不全选")
radios[i].checked = true;
}
}
}
</script>
</head>
<body>
<input type="radio" name="radio" onclick="selectRadio(1)" value="全选"/>全选<br/>
<input type="radio" name="radio" onclick="selectRadio(2)" value="不全选" />不全选<br/><input type="checkbox" name="checkbox" onclick="selectCheckbox()">1
<input type="checkbox" name="checkbox" onclick="selectCheckbox()">2</body>
</html>