简体   繁体   English

复选框在具有onclick侦听器的div中不可单击

[英]Checkbox is unclickable inside div that has onclick listener

I have a checkbox inside a div that has an onclick javascript event attached to it. 我在div中有一个复选框,该复选框具有附加的onclick javascript事件。

When I click on the checkbox, the onclick event is fired, instead of the checkbox being checked. 当我单击复选框时,将触发onclick事件,而不是选中该复选框。

Any ideas on how to fix this behaviour? 关于如何解决此行为的任何想法?

Thanks 谢谢

You can try to change your code (in the jsfiddle.net example) to something like this : 您可以尝试将代码(在jsfiddle.net示例中)更改为类似以下内容

$(function() {
    $('#click').click(function(event) {
        var $target = $(event.target);
        if (!$target.is(':input')) {
            // div and contents, except inputs
            alert('click');
        } else {
            // div's content inputs
        }
    });
});

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

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