我创建了以下脚本来选择/取消选择多个复选框并将选定的复选框值存储在隐藏字段中。现在我需要你的小帮助来完成这些事情。当用户取消选择任何复选框时,我希望取消选择的复选框值执行某些任务。
我的 JS 代码:
jQuery.noConflict();
function toggle_chkbox(source,group,lbl) {
checkboxes = document.getElementsByName(group);
for(var i=0;i<checkboxes.length;i++)
{
var chkid = '#'+lbl+i;
checkboxes[i].checked = source.checked;
if(jQuery(chkid).hasClass('c_on') && checkboxes[i].checked == false )
{
jQuery(chkid).removeClass('c_on').addClass('c_off');
}
else if(jQuery(chkid).hasClass('c_off') && checkboxes[i].checked == true )
{
jQuery(chkid).removeClass('c_off').addClass('c_on');
}
}
updateTextArea();
}
function updateTextArea() {
var allVals = [];
jQuery('.taglist :checked').each(function(i) {
allVals.push((i!=0?"":"")+ jQuery(this).val());
});
jQuery('#textValue').val(allVals).attr('rows', allVals.length);
}
jQuery(function() {
jQuery('.taglist input').click(updateTextArea);
});
我的实时代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Checkbox</title>
<style type="text/css">
.has-js .label_check, .has-js .label_radio {
padding-left: 34px;
}
.has-js .label_check {
background: url(http://webdesign.maratz.com/lab/fancy-checkboxes-and-radio-buttons/check-off.png) no-repeat;
}
.has-js label.c_on {
background: url(http://webdesign.maratz.com/lab/fancy-checkboxes-and-radio-buttons/check-on.png) no-repeat;
}
.has-js .label_check input, .has-js .label_radio input {
position: absolute;
left: -9999px;
}
</style>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
</head>
<body>
<script type="text/javascript">
jQuery.noConflict();
function toggle_chkbox(source,group,lbl) {
checkboxes = document.getElementsByName(group);
for(var i=0;i<checkboxes.length;i++)
{
var chkid = '#'+lbl+i;
checkboxes[i].checked = source.checked;
if(jQuery(chkid).hasClass('c_on') && checkboxes[i].checked == false )
{
jQuery(chkid).removeClass('c_on').addClass('c_off');
}
else if(jQuery(chkid).hasClass('c_off') && checkboxes[i].checked == true )
{
jQuery(chkid).removeClass('c_off').addClass('c_on');
}
}
updateTextArea();
}
function updateTextArea() {
var allVals = [];
jQuery('.taglist :checked').each(function(i) {
allVals.push((i!=0?"":"")+ jQuery(this).val());
});
jQuery('#textValue').val(allVals).attr('rows', allVals.length);
}
jQuery(function() {
jQuery('.taglist input').click(updateTextArea);
});
</script>
<div class="taglist">
<label class="label_check" for="checkbox-01" id="lbl-box-posts1"><input name="ck-box-posts[]" id="checkbox-01" class="chkBX" value="1" type="checkbox" /> 2D Animation </label>
<br />
<label class="label_check" for="checkbox-02" id="lbl-box-posts2"><input name="ck-box-posts[]" id="checkbox-02" class="chkBX" value="2" type="checkbox" /> 3D Animation </label>
<br />
<label class="label_check" for="checkbox-03" id="lbl-box-posts3"><input name="ck-box-posts[]" id="checkbox-03" class="chkBX" value="3" type="checkbox" /> Animatronics </label>
<br />
<label class="label_check" for="checkbox-04" id="lbl-box-posts4"><input name="ck-box-posts[]" id="checkbox-04" class="chkBX" value="4" type="checkbox" /> Architectural </label>
<br />
<label class="label_check" for="checkbox-05" id="lbl-box-posts5"><input name="ck-box-posts[]" id="checkbox-05" class="chkBX" value="5" type="checkbox" /> Cartoon </label>
<br />
<label class="label_check" for="checkbox-06" id="lbl-box-posts6"><input name="ck-box-posts[]" id="checkbox-06" class="chkBX" value="all" type="checkbox" onchange="toggle_chkbox(this,'ck-box-posts[]','lbl-box-posts');" /> Check All </label>
<br />
<input type="hidden" name="textValue" id="textValue" >
</div>
<script type="text/javascript">
var d = document;
var safari = (navigator.userAgent.toLowerCase().indexOf('safari') == 1) ? true : false;
var gebtn = function(parEl,child) { return parEl.getElementsByTagName(child); };
jQuery.noConflict();
jQuery(document).ready(function() {
var body = gebtn(d,'body')[0];
body.className = body.className && body.className != '' ? body.className + ' has-js' : 'has-js';
if (!d.getElementById || !d.createTextNode) return;
var ls = gebtn(d,'label');
for (var i = 0; i < ls.length; i++) {
var l = ls[i];
if (l.className.indexOf('label_') == -1) continue;
var inp = gebtn(l,'input')[0];
if (l.className == 'label_check') {
l.className = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off';
l.onclick = check_it;
};
if (l.className == 'label_radio') {
l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
l.onclick = turn_radio;
};
};
});
var check_it = function() {
var inp = gebtn(this,'input')[0];
if (this.className == 'label_check c_off' || (!safari && inp.checked)) {
this.className = 'label_check c_on';
if (safari) inp.click();
} else {
this.className = 'label_check c_off';
if (safari) inp.click();
};
};
var turn_radio = function() {
var inp = gebtn(this,'input')[0];
if (this.className == 'label_radio r_off' || inp.checked) {
var ls = gebtn(this.parentNode,'label');
for (var i = 0; i < ls.length; i++) {
var l = ls[i];
if (l.className.indexOf('label_radio') == -1) continue;
l.className = 'label_radio r_off';
};
this.className = 'label_radio r_on';
if (safari) inp.click();
} else {
this.className = 'label_radio r_off';
if (safari) inp.click();
};
};
</script>
</body>
</html>
很抱歉没有创建 jsFiddle 或 jsbin,因为我收到了错误。:(
有什么想法或建议吗?谢谢。