Skip to content Skip to sidebar Skip to footer

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' , f

Solution 1:

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.

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.

Solution 2:

you have to change all these lines $('#' + $classname).removeAttr('checked'); to, and try by using .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);
                }
            });

        }elseif ($('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);
            }
        });
    }
});

Post a Comment for "Check And Uncheck Checkbox Not Working Correctly"