简体   繁体   English

选中和取消选中复选框无法正常工作

[英]Check and uncheck checkbox not working correctly

//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');
            }
        });
    }
});

Thats the jQuery I'm using to do the checks. 多数民众赞成在我用来做检查的jQuery。 And below is how my form is setup. 下面是我的表单设置方式。

Work Direction (selection): 工作方向(选择):

  • Domestic 国内
  • Offshore 离岸
  • Mexico 墨西哥

Departments Affected (checkboxes) ? 受影响的部门(复选框)?

  • [] CAD [] CAD
  • [] Design []设计
  • [] Shop []店铺
  • [] CNC [] CNC
  • [] Accounting []会计
  • [] Offshore []离岸
  • [] Mexico []墨西哥

Department Lineups (checkboxes) ? 部门阵容(复选框)?

  • [] c - CAD [] c-CAD
  • [] d - DESIGN [] d-设计
  • [] s - SHOP []秒-SHOP
  • [] m - CNC []米-CNC

So the logic is that, when Work Direction is Domestic or Offshore and c, d, s, m are checked off on the bottom, the top ones that should get checked off should be CAD, DESIGN, SHOP, and CNC. 因此,逻辑是,当“工作方向”为“国内”或“离岸”并且在底部选中c,d,s,m时,应选中的顶部应该是CAD,DESIGN,SHOP和CNC。 However, if Mexico is selected and c, d, s, or m are checked off, the top ones that should get checked off are CAD, DESIGN, MEXICO but not SHOP and CNC. 但是,如果选择了墨西哥并且选中了c,d,s或m,则应该选中的最上面的是CAD,DESIGN,MEXICO,但不是SHOP和CNC。

Now sometimes CAD and DESIGN won't be affected so if Mexico is selected, almost always SHOP or CNC will be affected so if user selects s, or m at the bottom, Mexico above should get checked off but not SHOP or CNC. 现在,有时CAD和DESIGN不会受到影响,因此,如果选择了Mexico,几乎总是会影响SHOP或CNC,因此,如果用户在底部选择s或m,则应取消选中上面的Mexico而不是SHOP或CNC。

I hope my explanation isn't too confusing but I'm not sure why my jQuery isn't working. 我希望我的解释不会太混乱,但是我不确定为什么我的jQuery无法正常工作。 Right now even if Mexico is selected and c, d, s, or m are selected, it'll check off CAD, DESIGN, SHOP, CNC, as well as MEXICO in the departments affected. 现在,即使选择了墨西哥并且选择了c,d,s或m,它也会在受影响的部门中检查CAD,DESIGN,SHOP,CNC和MEXICO。

Try 尝试

$('#work_direction').change(function() { ... });
var callback = function () { ... };
$('.shop, .cnc').unbind('click', callback);
$('.shop, .cnc').bind('click', callback);

Finally, you may or may not run into issues using attr(), use prop() instead. 最后,使用attr()可能会或可能不会遇到问题,请改用prop()。

Edit 编辑

Assuming your callback is the same: 假设您的回调相同:

var callback = function() {
    $classname = $(this).attr('class');
    if($('.' + $classname + ":checked").length > 0) {
        $('#mexico').attr('checked','checked');
    } else {
        $('#' + $classname).removeAttr('checked');
    }
};

You may now attach it and detach as needed: 您现在可以将其连接并根据需要分离:

$('.shop, .cnc').unbind('click', callback);
$('.shop, .cnc').bind('click', callback);

This ensures it only gets called once. 这样可以确保只调用一次。 I usually wrap this around a helper object that can unit test. 我通常将它包装在可以进行单元测试的辅助对象周围。

you have to change all these lines $('#' + $classname).removeAttr('checked'); 您必须更改所有这些行$('#' + $classname).removeAttr('checked'); to, and try by using .prop() 并尝试使用.prop()

 $('.' + $classname).prop('checked',false);

class notation is '.' 类标记为“。” not '#' change like below 不是'#'更改如下

//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);
            }
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM