简体   繁体   English

当我们单击子复选框时,如何获取父母的父母ID

[英]How to get the parent id of the parent when we click on the child check box

i am having an kendo treeview with checkbox template. 我正在使用带有复选框模板的剑道树视图。

my html code 我的html代码

<div id="treeview-left" style=" height: 375px; position: absolute;top: 30px;"></div>

I have defined the treeview like: 我已经定义了树视图,如:

var treeview1 = $('#treeview-left').kendoTreeView({
            select: onSelect,
            checkboxTemplate: kendo.template($("#treeview-checkbox-template").html()),            
            dragAndDrop: true,
            dataSource: parent, 
            dataTextField: ["parentName", "childName"]
        }).data("kendoTreeView");

And the checkbox template is defined like: 复选框模板的定义如下:

<script id="treeview-checkbox-template" type="text/kendo-ui-template">
            <input type="checkbox" />
</script>

And the datasource will be 数据源将是

var parent = new kendo.data.HierarchicalDataSource({
            type: "POST",
            dataType: "json",
            transport: {
                read: {
                    url: "/Projects/leftdisplay"
                }
            },
            schema: {
                model: {
                    id: "parentid",
                    hasChildren: "childName",
                    children: child
                }
            }
        });

now the child of the parent will be like: 现在父母的孩子会像:

   var child = {
            type: "POST",
            dataType: "json",
            transport: {
                read: {
                    url: function (options) {
                        return kendo.format("/projects/modulesleftdisplay?parentid=" + options.parentid + "&roleid=" + rolevalue + "&projectid=" + projectid + "");
                    }
                }
            },
            schema: {
                model: {
                    id: "childid",
                    hasChildren: function () {
                        return false;
                    }
                }
            }
        };

Now when i am clicking on the child checkbox i want to get the parent id of the selected child. 现在,当我单击子代复选框时,我想获取所选子代的父代ID。 How can i get that. 我该怎么办。

I have written code like 我写的代码像

$("#treeview-left [type=checkbox]").live('change', function (e) {      
    var chkBox = $(this)
    var parentid = chkBox.parent();
    var date = $('#treeview-left').data('kendoTreeView').dataItem(parent_id);
    var parid = date.id;
    var childid = $('#treeview-left').data('kendoTreeView').dataItem(parentid);
});

but the parid is not getting. 但没有得到成功。 How can i get the parent id of the selected child. 我如何获取所选孩子的父母ID。 Any help is appreciated. 任何帮助表示赞赏。

Try this: 尝试这个:

// Find all the checkboxes:
var checkboxes = Array.prototype.slice.call(document.querySelectorAll('#yourFormsID input[type=checkbox]'));

checkboxes.forEach(function(checkbox) {                //<== loop through checkboxes
    checkbox.addEventListener('change', function() {   //<== When clicked:
        console.log(this.parentNode.id);               //<== Log the id of the checkbox's parentNode in the console.
    }, false);
});

Here's a demo . 这是一个演示 Be sure to open the console. 确保打开控制台。

you can use this 你可以用这个

   var parentId = $(this).parent().closest(parent element).attr('id')

Example: 例:

 var parentId  = $(this).parent().closest('li').attr('id')

instead of 代替

 var chkBox = $(this)

 var parentid = chkBox.parent();

to get the parent id. 获取父ID。

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

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