kendo ui取消选中时获取复选框的ID [英] kendo ui get id of checkbox when unchecked

查看:137
本文介绍了kendo ui取消选中时获取复选框的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有复选框的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

这是kendo ui的矿藏代码

this is kendo ui mine code

// 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 this jquery code

$('input[type=checkbox]').click(function() {
    if($(this).is(':checked')) {
       alert('checked');
    } else {
        alert('not checked');
    }
});

此代码仅适用于js小提琴,但不适用于我的情况 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 id thanks 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:

  1. 找到最接近的li元素,它是被单击的树的节点.
  2. 该ID位于hidden的HTML input元素中(这是我了解的存储方式).
  3. 使用dataSource.get方法从dataSource获取项目.
  1. find the closest li element that is the node of the tree that has been clicked.
  2. the id is in an HTML input element that is hidden (this is the way that I understood that you have stored it).
  3. Get item from dataSource using dataSource.get method.

此处

这篇关于kendo ui取消选中时获取复选框的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆