【问题标题】:Click a li to check / uncheck a checkbox单击 li 以选中/取消选中复选框
【发布时间】:2011-09-08 09:47:21
【问题描述】:

我应用了下一个代码来使 li 在单击时选中/取消选中子复选框。问题是当单击复选框本身时它不起作用。我已经阅读了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,带有一个子复选框。没什么特别的。

【问题讨论】:

  • 你能把一个 jsfiddle 放在这个案例中,以便我可以调试它吗?!
  • Dosent 很有意义...您还应该使用 attr('checked', true/false) 而不是 checked 和 empty :) 尝试在其上使用 firebug,看看我在哪里失败了?跨度>

标签: jquery checkbox


【解决方案1】:

尝试改变 -

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

到 -

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多