单击li以选中/取消选中复选框

时间:2011-09-08 09:47:22

标签: jquery checkbox

我点击后应用了下一个代码来检查/取消选中子复选框。问题是当单击复选框本身时它不起作用。我已经阅读了the post here并试图应用e.stopPropagation();但仍然没有工作,有什么想法吗?

 $('#columnList li').click(function(){
            var check = $(this).children();
            var checkVal = check.attr('value');
            var helper = $('.' + checkVal);
            console.log(helper);
            if(check.is(':checked')){
                check.attr('checked','');
                $.each(helper, function(i, e){
                    $(helper[i]).hide();
                });
            }else{
                check.attr('checked','checked');
                $.each(helper, function(i, e){
                    $(helper[i]).show();
                });
           } 
        });

        $('input[type="checkbox"]').click(function(){
            e.stopPropagation();
        });

HTML是一个普通的ul,带有子复选框。没什么特别的。

2 个答案:

答案 0 :(得分:4)

尝试更改 -

$('input[type="checkbox"]').click(function(){
            e.stopPropagation();
        });

到 -

$('input[type="checkbox"]').click(function(e){
            e.stopPropagation();
        });

答案 1 :(得分:2)

您应该尝试将事件传递给函数

$('input[type="checkbox"]').click( function(e) {
    e.stopPropagation();
});
相关问题