JQuery将click事件绑定到复选框

iJa*_*red 9 jquery bind

这是我的html的基本视图:

<form>
  <div id="part1">
     // some html form elements including checkboxes
  </div>
  <div id="part2">
    // more html code with more checkboxes
  </div>
  <div id=part2">
     // data table with numerous checkboxes built dynamically
  </div
</form>
Run Code Online (Sandbox Code Playgroud)

我需要做的是将.click()事件分别绑定到第1部分,第2部分和第3部分中的复选框.我试过这个$('#part1:checkbox').click(...)但是没用.如果我使用$("form :checkbox").click(...)相同的点击事件绑定到所有复选框而不仅仅是一组.我怎么分开这些?

Jos*_*ber 21

你快到了.你只需要一个空格来分隔后代选择器:

$('#part1 :checkbox').click(function(){
    // code goes here
});
Run Code Online (Sandbox Code Playgroud)

为了提高性能,您可能想要使用它:

$('#part1 input[type=checkbox]').click(function(){
    // code goes here
});
Run Code Online (Sandbox Code Playgroud)