实在是找不到原因,只好求助---------JavaScript全选与不全选复选框

用户昵称不能为空 2009-12-01 06:14:32
//全国
function selectAllCity(){
for(var pid in cityArr){
selectCity(pid);
}
}

//全国城市都显示隐藏
function showAllCity(){
for(var pid in cityArr){
showCityList(pid);
}
}


showAllCity()可以,但是selectAllCity()却不可以。
HTML代码如下:(大家可以本地复制试一下,在全国那里点击展示和隐藏确实可以的,但是就是选择全国所有的城市就是不行)



<style type="text/css">
<!--
div,
div div,
div div div {
margin-left: 15px;
display:block;
}
-->
</style>

<label><input name="china" type="checkbox" id="china" value="all" onclick="selectAllCity()">
全国</label><span class="kw"><a href="javascript:;" onclick="showAllCity()">(展开/隐藏所有城市)</a></span>

<div id="prolist_1">
<label><input name="pro[]" type="checkbox" id="pro_1" value="1">
</label>北京 
<span class="kw"><a href="javascript:;" onClick="showCityList(1)">展开/隐藏</a></span>
<div id="citylist_1" style="display:none">
<label>
<input name="city[]" type="checkbox" id="city_1" value="1">
北京 </label><br>

</div><br>
<label><input name="pro[]" type="checkbox" id="pro_2" value="2">
</label>上海 
<span class="kw"><a href="javascript:;" onClick="showCityList(2)">展开/隐藏</a></span>
<div id="citylist_2" style="display:none">
<label>
<input name="city[]" type="checkbox" id="city_2" value="2">
上海 </label><br>

</div><br>
<label><input name="pro[]" type="checkbox" id="pro_3" value="3">
</label>天津 
<span class="kw"><a href="javascript:;" onClick="showCityList(3)">展开/隐藏</a></span>
<div id="citylist_3" style="display:none">
<label>
<input name="city[]" type="checkbox" id="city_3" value="3">
天津 </label><br>

</div><br>
<label><input name="pro[]" type="checkbox" id="pro_4" value="4">
</label>重庆 
<span class="kw"><a href="javascript:;" onClick="showCityList(4)">展开/隐藏</a></span>
<div id="citylist_4" style="display:none">
<label>
<input name="city[]" type="checkbox" id="city_4" value="4">
重庆 </label><br>

</div><br>
<label><input name="pro[]" type="checkbox" id="pro_5" value="5">
</label>安徽 
<span class="kw"><a href="javascript:;" onClick="showCityList(5)">展开/隐藏</a></span>
<div id="citylist_5" style="display:none">
<label>
<input name="city[]" type="checkbox" id="city_5" value="5">
合肥 </label><br>

<label>
<input name="city[]" type="checkbox" id="city_6" value="6">
黄山 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_7" value="7">
芜湖 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_8" value="8">

马鞍山 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_9" value="9">
安庆 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_10" value="10">
淮南 </label><br>

<label>
<input name="city[]" type="checkbox" id="city_11" value="11">
阜阳 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_12" value="12">
蚌埠 </label><br>
</div><br>
<label><input name="pro[]" type="checkbox" id="pro_6" value="6">

</label>福建 
<span class="kw"><a href="javascript:;" onClick="showCityList(6)">展开/隐藏</a></span>
<div id="citylist_6" style="display:none">
<label>
<input name="city[]" type="checkbox" id="city_13" value="13">
福州 </label><br>
<label>

<input name="city[]" type="checkbox" id="city_14" value="14">
厦门 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_15" value="15">
泉州 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_16" value="16">
漳州 </label><br>

<label>
<input name="city[]" type="checkbox" id="city_17" value="17">
龙岩 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_18" value="18">
三明 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_19" value="19">

南平 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_20" value="20">
宁德 </label><br>
<label>
<input name="city[]" type="checkbox" id="city_21" value="21">
莆田 </label><br>

</div><br>




<script language="javascript" type="text/javascript">


//create date 2009-12-01 16:47:20
var cityArr=new Array();
cityArr[1]=[1];
cityArr[2]=[2];
cityArr[3]=[3];
cityArr[4]=[4];
cityArr[5]=[5,6,7,8,9,10,11,12];
cityArr[6]=[13,14,15,16,17,18,19,20,21];

function G(ID){
return document.getElementById(ID);
}

//全国
function selectAllCity(){
for(var pid in cityArr){
selectCity(pid);
}
}

//全国城市都显示隐藏
function showAllCity(){
for(var pid in cityArr){
showCityList(pid);
}
}

//选取城市列表
function selectCity(pid){
var pchecked=G("pro_"+pid).checked;
G("citylist_"+pid).style.display=(pchecked==true)?'':'none';
for(var c in cityArr[pid]){
G("city_"+cityArr[pid][c]).checked=(pchecked==true)?"checked":"";
}
}
//展现城市
function showCityList(pid){
var obj=G("citylist_"+pid).style;
obj.display=(obj.display)? '':'none';
return;
}

window.onload=function(){
var inputArr=document.getElementsByTagName("input");
for(var i=0;i< inputArr.length;i++){
if(inputArr[i].type!="checkbox"){continue;}//跳过
if(!(/^pro/.test(inputArr[i].id))){continue;}//跳过
inputArr[i].onclick=function(){
selectCity(this.value);//展现城市
}
}
}
</script>

...全文
137 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
luohuayh 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zswang 的回复:]
selectCity中是通过G("pro_"+pid).checked判断,但selectAllCity中没有改变这个属性。

JScript codefunction selectAllCity(){for(var pidin cityArr){
G("pro_"+pid).checked= G("china").checked;
selectCity(pid);
}
}
[/Quote]


正解
王集鹄 2009-12-01
  • 打赏
  • 举报
回复
selectCity中是通过G("pro_"+pid).checked判断,但selectAllCity中没有改变这个属性。

    function selectAllCity(){
for(var pid in cityArr){
G("pro_"+pid).checked = G("china").checked;
selectCity(pid);
}
}

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧