单击div选中/取消选中复选框
问题描述:
我应用了以下代码来使表行在单击时选中/取消选中子复选框。现在,我发现单击行中的复选框本身不会进行检查。可能是它检查click(标准功能),然后jquery拾取click事件并取消选中它吗?我该如何解决?
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 doesent check. Could it be that it checks on click (standard function) and then the jquery picks up the click event and unchecks it? How can I fix this?
//check on div click
$("tr").live("click",function() {
var checkbox = $(this).find("input[type='checkbox']");
if( checkbox.attr("checked") == "" ){
checkbox.attr("checked","true");
} else {
checkbox.attr("checked","");
}
});
答
我怀疑click事件正在从复选框中冒泡到tr。尝试同时添加以下代码:
I suspect that the click event is bubbling from the checkbox to the tr. Try adding this code as well:
$('tr input[type=checkbox]').click(function(e){
e.stopPropagation();
});
编辑
这是一个示例: http://jsfiddle.net/GSqNv/