jQuery通过id从id获取id

时间:2017-02-10 08:36:13

标签: javascript jquery html

我想通过div中的id复选框找到一个onClick事件(也应该通过id找到它)。

我的代码是:

$('#myDivId').find('#myCheckboxId').change(function() {
            if (this.checked) {
            }
            else{
            }
});

但出了点问题。我知道页面中应该有一个Id,但是有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

$('#myDivId>#myCheckboxId').change(function() {
 if (this.checked) {
   console.log('checked!');
 }
 else{
   console.log('not checked');
 }
});

示例是here