jQuery将单击事件绑定到复选框 [英] JQuery bind click event to checkboxes

查看:102
本文介绍了jQuery将单击事件绑定到复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的html的基本视图:

Here's a basic view of what my html looks like:

<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>

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

What I need to do is bind the .click() event to the checkboxes in part 1, 2, and 3 seperately. I've tried this $('#part1:checkbox').click(...) but that didn't work. If I use $("form :checkbox").click(...) same click event is bound to all checkboxes and not just one set. How would I seperate these?

推荐答案

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

You're almost there. You just need a space to separate the descendant selector:

$('#part1 :checkbox').click(function(){
    // code goes here
});

要提高性能,您可能要使用此功能:

To increase performance, you might wanna use this:

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

这篇关于jQuery将单击事件绑定到复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆