Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改div中的任何复选框都会触发函数_Javascript_Jquery_Html_Css_Checkbox - Fatal编程技术网

Javascript 更改div中的任何复选框都会触发函数

Javascript 更改div中的任何复选框都会触发函数,javascript,jquery,html,css,checkbox,Javascript,Jquery,Html,Css,Checkbox,假设一个div中有多个复选框(称之为“startwars”),我知道如果复选框的状态更改或在整个页面上单击,如何检查和触发函数,方法如下: $('input[type=checkbox]').click(function() { console.log("somethign is checked on the page") }); 但是,如果我想将该范围限制为一个div,并检查是否更改/单击了该div中的任何复选框状态,该怎么办 如果在使用JQuery的div元素中更改/单击了任何复选

假设一个div中有多个复选框(称之为“startwars”),我知道如果复选框的状态更改或在整个页面上单击,如何检查和触发函数,方法如下:

$('input[type=checkbox]').click(function() {
    console.log("somethign is checked on the page")
});
但是,如果我想将该范围限制为一个div,并检查是否更改/单击了该div中的任何复选框状态,该怎么办


如果在使用JQuery的div元素中更改/单击了任何复选框,我想触发一个函数,向要限制其范围的div添加一个类或id,并在JQuery的选择器中使用它

<div class='limited'>
    <input type='checkbox' />
</div

您可以通过向希望事件在其中发生的div添加id或类来完成此操作,如下所示:


$(“#myDivId输入[type=checkbox]”):-)非常有助于
。更改
。单击你解释的差异,很好!
$('.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")
});
<div id="slct"">
<h1>
INSIDE
</h1>
  <input type="checkbox">
  <input type="checkbox">
  <input type="checkbox">
</div>
<div>
<h1>
OUTSIDE
</h1>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
<script>
$('#slct input[type=checkbox]').change(function(){ 
alert("See it's working");
});
</script>