简体   繁体   English

单击父级时的jQuery切换复选框

[英]jQuery toggle checkbox when parent is clicked

I have written a simple js function to toggle the checkbox on/off then the parent (in this case a TD) is clicked. 我编写了一个简单的js函数来打开/关闭复选框,然后单击父对象(在这种情况下为TD)。 This function works as expected when you click anywhere on the TD . 当您在TD上的任意位置单击时,此功能将按预期工作

However when you click on the checkbox itself , nothing happens. 但是,当您单击复选框本身时 ,什么也不会发生。
What I would like to know is why it does this and what's the best work around? 我想知道为什么会这样做,最好的解决方法是什么?

I have a provided a jsfiddle to demo this. 我提供了一个jsfiddle进行演示。

And here is my js function: 这是我的js函数:

$("td.onoff").click(function() {
    var objCheckbox = $(this).find("input[type=checkbox]");
    if( objCheckbox.length >= 1 ) {
        objCheckbox.prop("checked", !objCheckbox.prop("checked"));
    }
});

Thanks. 谢谢。

You need to by pass event code when source of event is not td with class onoff , You can do this way. by pass event code when source of event is not td with class onoff您需要by pass event code when source of event is not td with class onoff ,您可以这样做。

Live Demo 现场演示

$(function() {
    $("td.onoff").click(function(event) {          
        if(event.target.className != 'onoff') return;
        var objCheckbox = $(this).find("input[type=checkbox]");
        if( objCheckbox.length >= 1 ) {
            objCheckbox.prop("checked", !objCheckbox.prop("checked"));
        }
    });
});

check the type of clicked element. 检查单击的元素的类型。 Without this, you are trying to get an element that not exist. 没有这个,您将试图获得一个不存在的元素。

$(function() {
    $("td.onoff").click(function() {
        var type = $(this).attr('type');
        if ( type != "checkbox" ) {
            var objCheckbox = $(this).find("input[type=checkbox]");
            if( objCheckbox.length >= 1 ) {
                objCheckbox.prop("checked", !objCheckbox.prop("checked"));
            }
        }
    });
});​   

The reason it behaves in this way is because the input is triggering the parent click event, at which point this becomes the checkbox, and not the clicked td , so .find("input[type=checkbox]") won't select anything, and therefore no action will be performed. 它以这种方式运行的原因是因为input触发了父click事件,这时this成为复选框,而不是被单击的td ,因此.find("input[type=checkbox]")不会选择任何内容,因此将不执行任何操作。

An alternative to the other answers would be to bind the click event to the input directly, and stop it from propagating, eg: 其他答案的替代方法是将click事件直接绑定到输入,并阻止其传播,例如:

$("td.onoff > input").click(function(e) {
  e.stopPropagation();        
});

Here's a demo 这是一个演示

Add the following: 添加以下内容:

$("td.onoff :checkbox").click(function(event) {
    event.stopPropagation();
});

This prevents the click event from bubbling up to the container when you click on the checkbox. 当您单击复选框时,这可以防止click事件冒泡到容器中。

Use "for" attribute rather than any javascript function This will automatically work as it does by function: Just below html: 使用“ for”属性而不是任何javascript函数这将自动按函数运行:html下方:

<tr class="row" >
        <td class="onoff" ><label for="check1"><input id="check1" type="checkbox" name="something[]" checked="checked" /></label></td>
    </tr>
    <tr class="row">
        <td class="onoff"><label for="check2"><input id="check2" type="checkbox" name="something[]" checked="checked" /></td>
    </tr>
    <tr class="row">
        <td class="onoff"><label for="check3"><input id="check3" type="checkbox" name="something[]" checked="checked" /></td>
 </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM