checkbox 多选框 联动实现代码
程序员文章站
2023-12-30 11:08:52
父类 复制代码 代码如下:
父类
<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>
推荐阅读
-
checkbox 多选框 联动实现代码
-
real media editor php radio 单选框获取与保持值的实现代码
-
yii实现CheckBox复选框在同一行显示的方法
-
jQuery中的RadioButton,input,CheckBox取值赋值实现代码
-
yii实现CheckBox复选框在同一行显示的方法,yiicheckbox
-
代码非常简洁且兼容多浏览器的拖动层实现代码
-
在ecshop的购物流程页,商品列表加一个复选框,并实现与之相对应功能的代码,该怎么处理
-
jQuery判断checkbox(复选框)是否被选中以及全选、反选实现代码
-
input:checkbox多选框实现单选效果跟radio一样
-
js 判断checkbox是否选中的实现代码_javascript技巧