简体   繁体   English

jQuery按id从div中按id获取对象

[英]jQuery get object by id from div by id

I want to get an onClick event on found by id checkbox in div(should find it by id too).我想在 div 中通过 id 复选框找到一个 onClick 事件(也应该通过 id 找到它)。

My code is:我的代码是:

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

but something goes wrong.但出了点问题。 I know that there should be one Id in page but is there any way to solve this problem?我知道页面中应该有一个 Id 但有什么办法可以解决这个问题吗?

Try this:尝试这个:

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

Example is here .例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM