javascript 單選框,多選框美化代碼
更新時(shí)間:2008年08月01日 01:00:18 作者:
javascript checkbox,radio美好效果代碼,非常漂亮

crir = {
init: function() {
arrLabels = document.getElementsByTagName('label');
searchLabels:
for (var i=0; i<arrLabels.length; i++) {
// get the input element based on the for attribute of the label tag
if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value != '') {
labelElementFor = arrLabels[i].getAttributeNode('for').value;
inputElement = document.getElementById(labelElementFor);
}
else {
continue searchLabels;
}
inputElementClass = inputElement.className;
// if the input is specified to be hidden intiate it
if (inputElementClass == 'crirHiddenJS') {
inputElement.className = 'crirHidden';
inputElementType = inputElement.getAttributeNode('type').value;
// add the appropriate event listener to the input element
if (inputElementType == "checkbox") {
inputElement.onclick = crir.toggleCheckboxLabel;
}
else {
inputElement.onclick = crir.toggleRadioLabel;
}
// set the initial label state
if (inputElement.checked) {
if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_checked'}
else { arrLabels[i].className = 'radio_checked' }
}
else {
if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_unchecked'}
else { arrLabels[i].className = 'radio_unchecked' }
}
}
else if (inputElement.nodeName != 'SELECT' && inputElement.getAttributeNode('type').value == 'radio') { // this so even if a radio is not hidden but belongs to a group of hidden radios it will still work.
arrLabels[i].onclick = crir.toggleRadioLabel;
inputElement.onclick = crir.toggleRadioLabel;
}
}
},
findLabel: function (inputElementID) {
arrLabels = document.getElementsByTagName('label');
searchLoop:
for (var i=0; i<arrLabels.length; i++) {
if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value == inputElementID) {
return arrLabels[i];
break searchLoop;
}
}
},
toggleCheckboxLabel: function () {
labelElement = crir.findLabel(this.getAttributeNode('id').value);
if(labelElement.className == 'checkbox_checked') {
labelElement.className = "checkbox_unchecked";
}
else {
labelElement.className = "checkbox_checked";
}
},
toggleRadioLabel: function () {
clickedLabelElement = crir.findLabel(this.getAttributeNode('id').value);
clickedInputElement = this;
clickedInputElementName = clickedInputElement.getAttributeNode('name').value;
arrInputs = document.getElementsByTagName('input');
// uncheck (label class) all radios in the same group
for (var i=0; i<arrInputs.length; i++) {
inputElementType = arrInputs[i].getAttributeNode('type').value;
if (inputElementType == 'radio') {
inputElementName = arrInputs[i].getAttributeNode('name').value;
inputElementClass = arrInputs[i].className;
// find radio buttons with the same 'name' as the one we've changed and have a class of chkHidden
// and then set them to unchecked
if (inputElementName == clickedInputElementName && inputElementClass == 'crirHidden') {
inputElementID = arrInputs[i].getAttributeNode('id').value;
labelElement = crir.findLabel(inputElementID);
labelElement.className = 'radio_unchecked';
}
}
}
// if the radio clicked is hidden set the label to checked
if (clickedInputElement.className == 'crirHidden') {
clickedLabelElement.className = 'radio_checked';
}
},
addEvent: function(element, eventType, doFunction, useCapture){
if (element.addEventListener)
{
element.addEventListener(eventType, doFunction, useCapture);
return true;
} else if (element.attachEvent) {
var r = element.attachEvent('on' + eventType, doFunction);
return r;
} else {
element['on' + eventType] = doFunction;
}
}
}
crir.addEvent(window, 'load', crir.init, false);
在線演示http://img.jb51.net/online/checkbox/sample.html
打包下載

您可能感興趣的文章:
- AngularJS單選框及多選框?qū)崿F(xiàn)雙向動(dòng)態(tài)綁定
- ExtJS Grid使用SimpleStore、多選框的方法
- JavaScript判斷表單中多選框checkbox選中個(gè)數(shù)的方法
- ExtJS 下拉多選框lovcombo
- 用原生JS實(shí)現(xiàn)簡(jiǎn)單的多選框功能
- 兼容ie和firefox版本的js反選 全選 多選框
- 用 Javascript 驗(yàn)證表單(form)中多選框(checkbox)值
- html+javascript+bootstrap實(shí)現(xiàn)層級(jí)多選框全層全選和多選功能
- AngularJS 單選框及多選框的雙向動(dòng)態(tài)綁定
- JS實(shí)現(xiàn)多選框的操作
相關(guān)文章
javascript radio 聯(lián)動(dòng)效果
js聯(lián)動(dòng)效果實(shí)現(xiàn)代碼2009-03-03JavaScript 輸入框內(nèi)容格式驗(yàn)證代碼
當(dāng)鼠標(biāo)焦點(diǎn)移至密碼輸入框時(shí),利用js腳本自動(dòng)驗(yàn)證用戶名的格式正確與否2010-02-02js模擬權(quán)限選擇實(shí)現(xiàn)代碼(select操作)
權(quán)限選擇頁面,選擇、撤回、全部選擇、全部撤回2012-02-02javascript之文本框輸入四個(gè)數(shù)字自動(dòng)加空格的腳本
javascript之文本框輸入四個(gè)數(shù)字自動(dòng)加空格的腳本...2007-11-11判斷多個(gè)元素(RADIO,CHECKBOX等)是否被選擇的原理說明
多個(gè)元素(RADIO,CHECKBOX等)是否被選擇,常用的就是下面的方法,大家可以研究下。2009-02-02javascript 單選按鈕 單擊選中 雙擊取消選擇實(shí)現(xiàn)代碼
很多情況下,我們需要對(duì)radio單選框?qū)崿F(xiàn)取消選擇,這里用js的實(shí)現(xiàn)方法,確實(shí)不錯(cuò)。2010-08-08