问题描述
我正在使用带有复选框的 kendo ui 树视图
i am using kendo ui tree view with check box
我想要复选框被取消选中时的 ID
i want the check box's id when it is getting unchecked
这是剑道ui我的代码
// var homogeneous contains data
$("#treeview").kendoTreeView({
checkboxes: {
checkChildren: false,
template:"# if(!item.hasChildren){# <input type='hidden' id='#=item.id#' parent_id='#=item.parent_id#' d_text='#=item.value#'/> <input type='checkbox' id_a='#= item.id #' name='c_#= item.id #' value='true' />#}else{# <div id='#=item.id#' style='display:none;' parent_id='#=item.parent_id#' d_text='#=item.value#'/> #}#",
},
dataSource: homogeneous,
dataBound: ondata,
dataTextField: "value"
});
function ondata() {
//alert("databound");
}
// function that gathers IDs of checked nodes
function checkedNodeIds(nodes, checkedNodes) {
//console.log(nodes);
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].checked) {
checkedNodes.push(nodes[i].id);
}
if (nodes[i].hasChildren) {
checkedNodeIds(nodes[i].children.view(), checkedNodes);
}
}
}
// show checked node IDs on datasource change
$("#treeview").data("kendoTreeView").dataSource.bind("change", function() {
var checkedNodes = [],
treeView = $("#treeview").data("kendoTreeView"),
message;
checkedNodeIds(treeView.dataSource.view(), checkedNodes);
if (checkedNodes.length > 0) {
message = "IDs of checked nodes: " + checkedNodes.join(",");
} else {
message = "No nodes checked.";
}
$("#result").html(message);
});
在这段代码中,当它被取消选中时,我没有得到复选框的 id,所以我试过了jquery代码
in this code i am not getting checkbox's id when it is unchecked so i have tried thisjquery code
$('input[type=checkbox]').click(function() {
if($(this).is(':checked')) {
alert('checked');
} else {
alert('not checked');
}
});
此代码仅适用于 js fiddle 而不适用于我的情况 http://jsfiddle.net/NPUeL/一个>
this code is only working in js fiddle but not in my case http://jsfiddle.net/NPUeL/
如果我使用此代码,那么我可以获得计数,但我不知道如何使用它
if i use this code then i can get the number of count but i dont know how to use it
var treeview = $("[data-role=treeview]").data("kendoTreeView");
treeview.dataSource.bind("change", function (e) {
if (e.field == "checked") {
console.log("Recorded Selected: " + $("[data-role=treeview] :checked").length);
}
});
我需要在数据源中进行哪些更改才能获得 id提前致谢
what changed i need to do in data source so i can get idthanks in adavance
推荐答案
如果你想得到 id
你可以这样做:
If you want to get the id
you might do:
$('input[type=checkbox]').click(function (e) {
var li = $(e.target).closest("li");
var id = $("input:hidden", li).attr("id");
var node = treeView.dataSource.get(id);
if (node.checked) {
console.log('checked');
} else {
console.log('not checked');
}
});
我在事件处理程序中做的是:
What I do in the event handler is:
- 找到最近的
li
元素,它是被点击的树的节点. - id 位于
hidden
的 HTMLinput
元素中(据我所知,这是您存储它的方式). - 使用
dataSource.get
方法从dataSource
获取项目.
- find the closest
li
element that is the node of the tree that has been clicked. - the id is in an HTML
input
element that ishidden
(this is the way that I understood that you have stored it). - Get item from
dataSource
usingdataSource.get
method.
在此处
这篇关于kendo ui 在未选中时获取复选框的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!