【原创】jQuery获取TreeView选中节点的Text和Value值

本文介绍如何在前后端实现TreeView中节点的选择行为,包括节点存在和不存在行为事件时的处理方法,并提供具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、节点存在行为事件


后台代码:

1
2
3
4
5
6
7
8
// 子节点
TreeNode childNode = new TreeNode();
                                                  
childNode.Target = "main";
childNode.ShowCheckBox = true;
                                                  
childNode.Value = category.CategoryID;
childNode.Text = category.CategoryName;

前台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//获取节点Text和Value值
function GetNodeInfo() {
    //获取选中的checkbox
    //tv_category:TreeView控件ID
    var objs = $("#tv_category").find(":checkbox:checked");
                                               
    //装载Text和Value
    var select = [];
                                               
    //遍历选中checkbox
    $.each(objs, function() {
                                                   
        //获取列的href
        var href = $(this).next().attr("href");
                                                   
        //获取列的值
        var text = $(this).next().text();
                                                   
        //获得最后一次出现的索引
        var tempindex = href.lastIndexOf("\\") + 1;
                                                   
        //获得截取后的字符串
        var temphref= href.substring(tempindex);
                                                   
        //获得value值
        var hrefvalue = temphref.split('\'');
        var textvalue = text + "|" + hrefvalue[0];
                                                   
        select.push(textvalue);
    });
                                               
    alert(select.join("&"));
    //window.returnValue = select.join("&");
    //window.close();
}

2、节点不存在行为事件


后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
//子节点
TreeNode childNode = new TreeNode();
                                     
childNode.Target = "main";
childNode.ShowCheckBox = true;
//设置节点行为为空
childNode.SelectAction = TreeNodeSelectAction.None;
//將value赋值给NavigateUrl便于前台href取值
childNode.NavigateUrl = category.CategoryID;
                                     
childNode.Value = category.CategoryID;
childNode.Text = category.CategoryName;

前台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//获取节点Text和Value值
function GetNodeInfo() {
    //获取选中的checkbox
    var objs = $("#tv_category").find(":checkbox:checked");
                                       
    //装载Text和Value
    var select = [];
                                     
    //遍历选中checkbox
    $.each(objs, function() {
                                             
        //获取列的value
        var value = $(this).next().attr("href");
                                             
        //获取列的值
        var text = $(this).next().text();
                                             
        //Text和Value值
        var textvalue = text + "|" + value;
                                             
        select.push(textvalue);
    });
                                     
    alert(select.join("&"));
    //window.returnValue = select.join("&");
    //window.close();
}

效果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值