级联选择的一个例子

转自:http://blog.csdn.net/net_lover/article/details/6702027

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    <script type="text/javascript">  
    var nextInput = null;  
    var nextID = null;  
    var preInput = null;  
    var preID = null;  
    var uncheckParent = false;  
    var pId;  
  
    function checkIt(o) {  
      preInput = nextInput = o.checked;  
      nextID = o.id;  
      preID = o.getAttribute("parentId");  
      uncheckParent = false;  
      checkStart(o)  
    }  
  
    function checkStart(o) {  
      checkChild(o)  
      checkParent(o)  
    }  
  
    function checkChild(o) {  
      obj = o;  
      if (obj.nodeType == 1 && obj.tagName == "INPUT" && obj.type == "checkbox") {  
        if (obj.getAttribute("id").substr(0, nextID.length) == nextID)  
          obj.checked = nextInput          
      }  
      obj = obj.nextSibling;  
      if (obj == null) return;  
      checkChild(obj);  
    }  
  
    function checkParent(o) {  
      pId = o.getAttribute("parentId");  
      obj = o.previousSibling;  
      while (obj && obj.nodeType != 1 && obj.tagName != "INPUT" && obj.type != "checkbox") {  
        obj = obj.previousSibling;  
        if (obj == null) return;  
      }  
      if (obj == null) return;  
      if (obj.getAttribute("id") == preID) {            
        preID = obj.getAttribute("parentId");  
        obj.checked = preInput  
        if (uncheckParent) obj.checked = false;  
  
      }  
      //判断是否要取消父节点选择  
      chs = document.getElementById("tree").getElementsByTagName("INPUT");  
      for (i = 0; i < chs.length; i++) {  
        if (chs[i].getAttribute("parentId") == pId) {  
          if (chs[i].checked == false) {  
            uncheckParent = true;  
            break;  
          }  
        }  
      }  
      checkParent(obj);  
    }  
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div id="tree">
            <input type="checkbox" id="1" οnclick="checkIt(this)" parentid="0" />1<br />
            &nbsp;&nbsp;<input type="checkbox" id="11" οnclick="checkIt(this)" parentid="1" />11<br />
            &nbsp;&nbsp;<input type="checkbox" id="12" οnclick="checkIt(this)" parentid="1" />12<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="121" οnclick="checkIt(this)" parentid="12" />121<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="122" οnclick="checkIt(this)" parentid="12" />122<br />
            &nbsp;&nbsp;<input type="checkbox" id="13" οnclick="checkIt(this)" parentid="1" />13<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="131" οnclick="checkIt(this)" parentid="13" />131<br />
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="132" οnclick="checkIt(this)" parentid="13" />132<br />
            <input type="checkbox" id="2" οnclick="checkIt(this)" parentid="0" />2<br />
            &nbsp;&nbsp;<input type="checkbox" id="21" οnclick="checkIt(this)" parentid="2" />21<br />
            <input type="checkbox" id="3" οnclick="checkIt(this)" parentid="0" />3<br />
        </div>
    </form>
</body>
</html>

 

转载于:https://www.cnblogs.com/wifi/articles/3523547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值