checkbox 多选框 联动实现代码
程序员文章站
2023-12-01 15:37:04
父类 复制代码 代码如下:
父类
<input type="checkbox" name="father" forcheckboxgroup="groupname1"/>
子类
<input type="checkbox" name="son" group="groupname1"/>
实现代码
<script type="text/javascript">
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 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) {
master.indeterminate = false;
master.checked = true;
}
if (chknosel) {
master.indeterminate = false;
master.checked = false;
}
if(!chkselall&&!chknosel)master.indeterminate = true;
}
else{
master.disabled = chknosel;
}
}
}
window.attachevent('onload',checkboxgroup);
</script>
复制代码 代码如下:
<input type="checkbox" name="father" forcheckboxgroup="groupname1"/>
子类
复制代码 代码如下:
<input type="checkbox" name="son" group="groupname1"/>
实现代码
复制代码 代码如下:
<script type="text/javascript">
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 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) {
master.indeterminate = false;
master.checked = true;
}
if (chknosel) {
master.indeterminate = false;
master.checked = false;
}
if(!chkselall&&!chknosel)master.indeterminate = true;
}
else{
master.disabled = chknosel;
}
}
}
window.attachevent('onload',checkboxgroup);
</script>
上一篇: javascript TextArea动态显示剩余字符
下一篇: 会声会影怎么制作快镜头慢镜头特效?