Click a div to check / uncheck a checkbox

前端 未结 5 684
执念已碎
执念已碎 2020-12-11 19:13

I applied the following code to make table rows check/uncheck a child checkbox when clicked. Now I discovered that when clicking the checkbox itself inside the row it doesen

5条回答
  •  离开以前
    2020-12-11 19:51

    Check the target of the caller is not a checkbox

    $("tr").live("click",function(event) {
        var target = $(event.target);
        if (target.is('input:checkbox')) return;
    
        var checkbox = $(this).find("input[type='checkbox']");
    
        if( checkbox.attr("checked") == "" ){
           checkbox.attr("checked","true");
        } else {
           checkbox.attr("checked","");
        }
    });
    

    DEMO http://jsfiddle.net/7Bze7/

    The code above checks that the sender of the event is not a checkbox.

提交回复
热议问题