2015-12-23 27 views
4

假設div內有多個複選框(稱之爲「startwars」),我知道如果在複選框的狀態發生變化或點擊時檢查並觸發某個函數頁面做:更改div內的任何複選框觸發函數

​​

,但如果我想該範圍限制在一個div並檢查是否有任何複選框狀態的改變/點擊的是內部的一個div,

我想觸發一個函數,如果任何一個複選框被改變/單擊一個div元素與JQuery

+0

$( '#myDivId輸入[類型=複選框]'):-) – Ludo

回答

10

類或ID添加到要限制的範圍和jQuery中使用它在你的選擇

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

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") 
}); 
+0

Veryhelp爲'.change'和'你有.click'差異解釋說,尼斯! –

0

您可以通過添加一個id或類的做DIV要發生的事件是這樣的:https://jsfiddle.net/oa9wj80x/10/

<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> 
相關問題