chenjamer
chenjamer
2009-08-03 17:05
浏览 289

checkbox父子联动问题(一下午没搞定)

这是网上摘的一段代码,现在选择一个子checkbox父的不能被选中,我想要实现 在子checkbox中点击一个 父checkbox也被选中。
求教各位牛人

父类

子类


实现代码

//JS checkbox类型父子联动 function CheckboxGroup(){ var arrelement = document.all; var i=0; while(i<arrelement.length){ var forgroupattrib = arrelement[i].getAttribute('forcheckboxgroup'); if(forgroupattrib != null && forgroupattrib != ''){ arrelement[i].setAttribute('groupmember',_getGroupMember(arrelement[i])); if(arrelement[i].tagName.toLowerCase() == 'input' && arrelement[i].type == 'checkbox'){ arrelement[i].onclick=function(){ //----------------------Checked All------------------ var groupmember = this.getAttribute('groupmember'); var i = 0; while(i<groupmember.length){ groupmember[i].checked = this.checked; i++; } //--------------------------------------------------- } } _setState(arrelement[i]); } i++; } } function _getGroupMember(o){ var groupname = o.getAttribute('forcheckboxgroup'); var items = new Array; var inputs = document.getElementsByTagName('input'); var i=0; while(i<inputs.length){ if(inputs[i].type == 'checkbox'){ var groupattrib = inputs[i].getAttribute('group'); if(groupattrib == groupname){ items[items.length] = inputs[i]; var master = inputs[i].getAttribute('groupmaster'); if (master == null) { master = new Array; master[0] = o.uniqueID; inputs[i].setAttribute('groupmaster', master); } else{ master[master.length] = o.uniqueID; } inputs[i].onpropertychange = function(){ if (event.propertyName == 'checked') { var arro = this.getAttribute('groupmaster'); var i = 0; while (i < arro.length) { _setState(document.getElementById(arro[i])); i++; } } } } } i++; } return items; } function _setState(o){ var master = o; if(master!=null){ var chkselall = true; var chknosel = true; var chkselone = true; var groupmember = master.getAttribute('groupmember'); var i = 0; while(i<groupmember.length){ if(chkselall){ chkselall = groupmember[i].checked; } if(chknosel){ chknosel = !groupmember[i].checked; } i++; } if(master.tagName.toLowerCase() == 'input'&&master.type=='checkbox'){ if (chkselall || (!chkselall&&!chknosel)) { master.checked = true; } if (chknosel) { master.checked = false; } } else{ master.disabled = chknosel; } } } window.attachEvent('onload',CheckboxGroup);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • iteye_2251
    iteye_2251 2009-08-04 11:23
    已采纳

    [code="html"]



    function unionCheckbox(){ var fathers = document.getElementsByName("father"); if(fathers == 0)return; var father = fathers[0]; var sons = document.getElementsByName("son"); if(sons == 0)return; father.onchange = function(){ var checked = father.checked; eachSons(function(ckb){ ckb.checked = checked; }) } var eachSons = function(fun){ for(var i=0,l=sons.length; i<l; i++){ var ason = sons[i]; fun(ason); } } eachSons(function(ckb){ ckb.onchange = function(){ if(ckb.checked){//任何一个checkbox被选中,父checkbox也选中 if(!father.checked)father.checked = true; }else{ //如果子checkbox全部取消选择,则父checkbox也取消选择 var qty = 0; eachSons(function(ckb){ if(ckb.checked == false)qty++; }) if(qty == sons.length)father.checked = false; } } }) } unionCheckbox();



    [/code]

    点赞 评论

相关推荐