在更改div内的任何复选框时触发一个函数

Has*_*eer 7 html javascript css checkbox jquery

假设div中有多个复选框(称之为"startwars")我知道如果通过执行以下操作来改变或点击整个页面上的复选框状态,如何检查并触发函数:

$('input[type=checkbox]').click(function() {
    console.log("somethign is checked on the page")
});
Run Code Online (Sandbox Code Playgroud)

但是,如果我想将该范围限制为div并检查是否有任何复选框状态被更改/单击在div内部,

如果使用JQuery在div元素中更改/单击任何复选框,我想触发一个函数

Sol*_*gon 16

向想要限制范围的div添加一个类或id,并在jQuery中的选择器中使用它

<div class='limited'>
    <input type='checkbox' />
</div
Run Code Online (Sandbox Code Playgroud)

JS

$('.limited input[type=checkbox]').change(function() { // while you're at it listen for change rather than click, this is in case something else modifies the checkbox
    console.log("something is checked on the page")
});
Run Code Online (Sandbox Code Playgroud)