//if work direction = mexico and departments checked = Shop and CNC
//check off Mexico in Dept. Affected but don't check off Shop and CNC
$('#work_direction').live('click' , function() {
if ($('select[name^="workorder[work_direction]"]').val() == "mexico") {
$('.shop, .cnc').live('click', function(){
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0){
$('#mexico').attr('checked','checked');
} else {
$('#' + $classname).removeAttr('checked');
}
});
}else if ($('select[name^="workorder[work_direction]"]').val() == "domestic"){
$('.shop, .cnc').live('click', function(){
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0){
$('#' + $classname).attr('checked','checked');
} else {
$('#' + $classname).removeAttr('checked');
}
});
}else{
$('.cad, .design, .shop, .cnc').live('click', function(){
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0){
$('#' + $classname).attr('checked','checked');
} else {
$('#' + $classname).removeAttr('checked');
}
});
}
});这就是我用来做检查的jQuery。下面是我的表单的设置方式。
工作方向(甄选):
受影响的部门(复选框)?
部门排队(复选框)?
因此,逻辑是,当工作方向在国内或近海,c,d,s,m在底部被检查时,顶部应该被检查的是CAD、设计、车间和数控系统。但是,如果选择墨西哥,c,d,s或m被选中,则最需要检查的是CAD、DESIGN、墨西哥,而不是车间和CNC。
现在有些时候CAD和设计不会受到影响,所以如果选择墨西哥,几乎所有的车间或CNC都会受到影响,所以如果用户选择s,或m在底部,上面的墨西哥应该被检查,而不是商店或CNC。
我希望我的解释不会太混乱,但我不知道为什么我的jQuery不能工作。现在,即使墨西哥被选中,c,d,s或m被选中,它也会检查CAD、设计、车间、CNC以及墨西哥受影响的部门。
发布于 2014-03-28 12:04:03
试一试
$('#work_direction').change(function() { ... });
var callback = function () { ... };
$('.shop, .cnc').unbind('click', callback);
$('.shop, .cnc').bind('click', callback);最后,使用attr()可能会遇到问题,也可能不会遇到问题,可以使用prop()来代替。
编辑
假设您的回调是相同的:
var callback = function() {
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0) {
$('#mexico').attr('checked','checked');
} else {
$('#' + $classname).removeAttr('checked');
}
};您现在可以将它附加起来,并根据需要分离:
$('.shop, .cnc').unbind('click', callback);
$('.shop, .cnc').bind('click', callback);这确保它只被调用一次。我通常将它封装在一个可以进行单元测试的助手对象上。
发布于 2014-03-28 12:19:24
您必须将所有这些行$('#' + $classname).removeAttr('checked');改为,并使用.prop()进行尝试
$('.' + $classname).prop('checked',false);类表示法是“.”不是像下面这样的“#”变化
//if work direction = mexico and departments checked = Shop and CNC
//check off Mexico in Dept. Affected but don't check off Shop and CNC
$('#work_direction').live('click' , function() {
if ($('select[name^="workorder[work_direction]"]').val() == "mexico") {
$('.shop, .cnc').live('click', function(){
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0){
$('#mexico').prop('checked',true);
} else {
$('.' + $classname).prop('checked',false);
}
});
}else if ($('select[name^="workorder[work_direction]"]').val() == "domestic"){
$('.shop, .cnc').live('click', function(){
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0){
$('.' + $classname).prop('checked',true);
} else {
$('.' + $classname).prop('checked',false);
}
});
}else{
$('.cad, .design, .shop, .cnc').live('click', function(){
$classname = $(this).attr('class');
if($('.' + $classname + ":checked").length > 0){
$('.' + $classname).prop('checked',true);
} else {
$('.' + $classname).prop('checked',false);
}
});
}
});https://stackoverflow.com/questions/22712005
复制相似问题