简体   繁体   English

通过选中复选框获取特定的子元素ID

[英]get particular child element ID with selected the checkbox

I am using Knockout js for binding,i want to get child element ID,when i select the checkbox,my parent ID is generated by unique ID,how to achieve this 我正在使用Knockout js进行绑定,我想获取子元素ID,当我选中复选框时,我的父ID由唯一ID生成,如何实现

//this div is inside the for each loop

<div data-bind="attr: { id: 'unique_' + $index()},fav:  ({item:someItem})" class="Tab">

 // i need to get this itemIDs when i selecte the check box
   <div id=randon_item1><div>
   <div id=random_item2><div>
   <div id=random_item3><div>
   <div id=random_item4><div>
   </div>
    <input id="ckh1" type="checkbox" data-bind="value:userId(), click:      $root.toggleAssociation" />


 //toggleAssociation//
 self.toggleAssociation = function (item, event) {
 here i am getting the item and all parent div with unique_0 ID which will   be incremented for others div
 how i can get child element ID which is generated randomly
 }

I'm unfamiliar with knockout.js, but it appears you have some typos in your child elements id attributes, and you may want to contain that checkbox in the parent div with the class of "Tab" like this: 我不熟悉tickout.js,但似乎您的子元素id属性中有一些错别字,并且您可能希望在父div中使用“ Tab”类包含该复选框,如下所示:

<div data-bind="attr: { id: 'unique_' + $index()}, fav: ({item:someItem})" class="Tab">

    <div id="random_item1"></div>
    <div id="random_item2"></div>
    <div id="random_item3"></div>
    <div id="random_item3"></div>

    <input id="chk1" type="checkbox" data-bind="value:userId(), click: $root.toggleAssociation" />

</div>

Then your function: 然后你的功能:

self.toggleAssociation = function (item, event) {
    alert(event.target.parentNode.id);
}

or jQuery: 或jQuery:

self.toggleAssociation = function (item, event) {
    alert($(event).parent().attr(id));
}

if you want each of those "random_item" id's then you can iterate over them in your self.toggleAssociation function: 如果您想要每个“ random_item” ID,则可以在self.toggleAssociation函数中对其进行迭代:

$(event).siblings('div').each(function(){
    alert($(this).attr('id'));
});

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

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