49zTree - 带 checkbox 的多选下拉菜单

简介: 49zTree - 带 checkbox 的多选下拉菜单

效果图:

代码:

<!DOCTYPE html>
<HTML>
<HEAD>
  <TITLE> ZTREE DEMO - checkbox select menu</TITLE>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="../../../js/jquery.ztree.core.js"></script>
  <script type="text/javascript" src="../../../js/jquery.ztree.excheck.js"></script>
<!--  <script type="text/javascript" src="../../../js/jquery.ztree.exedit.js"></script>-->
  <SCRIPT type="text/javascript">
    <!--
    var setting = {
      check: {
        enable: true,
        chkboxType: {"Y":"", "N":""}
      },
      view: {
        dblClickExpand: false
      },
      data: {
        simpleData: {
          enable: true
        }
      },
      callback: {
        beforeClick: beforeClick,
        onCheck: onCheck
      }
    };
    var zNodes =[
      {id:1, pId:0, name:"北京"},
      {id:2, pId:0, name:"天津"},
      {id:3, pId:0, name:"上海"},
      {id:6, pId:0, name:"重庆"},
      {id:4, pId:0, name:"河北省", open:true, nocheck:true},
      {id:41, pId:4, name:"石家庄"},
      {id:42, pId:4, name:"保定"},
      {id:43, pId:4, name:"邯郸"},
      {id:44, pId:4, name:"承德"},
      {id:5, pId:0, name:"广东省", open:true, nocheck:true},
      {id:51, pId:5, name:"广州"},
      {id:52, pId:5, name:"深圳"},
      {id:53, pId:5, name:"东莞"},
      {id:54, pId:5, name:"佛山"},
      {id:6, pId:0, name:"福建省", open:true, nocheck:true},
      {id:61, pId:6, name:"福州"},
      {id:62, pId:6, name:"厦门"},
      {id:63, pId:6, name:"泉州"},
      {id:64, pId:6, name:"三明"}
     ];
    function beforeClick(treeId, treeNode) {
      var zTree = $.fn.zTree.getZTreeObj("treeDemo");
      zTree.checkNode(treeNode, !treeNode.checked, null, true);
      return false;
    }
    function onCheck(e, treeId, treeNode) {
      var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
      nodes = zTree.getCheckedNodes(true),
      v = "";
      for (var i=0, l=nodes.length; i<l; i++) {
        v += nodes[i].name + ",";
      }
      if (v.length > 0 ) v = v.substring(0, v.length-1);
      var cityObj = $("#citySel");
      cityObj.attr("value", v);
    }
    function showMenu() {
      var cityObj = $("#citySel");
      var cityOffset = $("#citySel").offset();
      $("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
      $("body").bind("mousedown", onBodyDown);
    }
    function hideMenu() {
      $("#menuContent").fadeOut("fast");
      $("body").unbind("mousedown", onBodyDown);
    }
    function onBodyDown(event) {
      if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
        hideMenu();
      }
    }
    $(document).ready(function(){
      $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });
    //-->
  </SCRIPT>
  <style type="text/css">
  </style>
 </HEAD>
<BODY>
<h1>带 checkbox 的多选下拉菜单  -- zTree</h1>
<h6>[ 文件路径: super/select_menu_checkbox.html ]</h6>
<div class="content_wrap">
  <div class="zTreeDemoBackground left">
    <ul class="list">
      <li class="title">&nbsp;&nbsp;<span class="highlight_red">勾选 checkbox 或者 点击节点 进行选择</span></li>
      <li class="title">&nbsp;&nbsp;Test: <input id="citySel" type="text" readonly value="" style="width:120px;" onclick="showMenu();" />
    &nbsp;<a id="menuBtn" href="#" onclick="showMenu(); return false;">select</a></li>
    </ul>
  </div>
  <div class="right">
    <ul class="info">
      <li class="title"><h2>实现方法说明</h2>
        <ul class="list">
        <li>用 checkbox 也可以轻松实现多选的下拉菜单</li>
        <li>此 Demo 主要用于 和 其他下拉菜单进行对比</li>
        </ul>
      </li>
    </ul>
  </div>
</div>
<div id="menuContent" class="menuContent" style="display:none; position: absolute;">
  <ul id="treeDemo" class="ztree" style="margin-top:0; width:180px; height: 300px;"></ul>
</div>
</BODY>
</HTML>
目录
相关文章
|
JavaScript 前端开发 Java
19jqGrid - 多选
19jqGrid - 多选
40 0
48zTree - 下拉菜单
48zTree - 下拉菜单
30 0
|
5月前
|
JavaScript
使用layui checkbox复选框样式实现单选功能
使用layui checkbox复选框样式实现单选功能
401 0
全选或者单选checkbox的值动态添加到div
全选或者单选checkbox的值动态添加到div
54 0
|
6月前
|
移动开发 JavaScript 小程序
uView Checkbox 复选框
uView Checkbox 复选框
134 0
复选框checkbox实现自定义样式
复选框checkbox实现自定义样式
64 1
50zTree - 带 radio 的单选下拉菜单
50zTree - 带 radio 的单选下拉菜单
49 1
|
6月前
|
JavaScript 前端开发
JS单选全选全不选
JS单选全选全不选
36 0
layui的复选框怎么设置只能单选
layui的复选框怎么设置只能单选
单选、全选、反选、获得所有选中的checkbox
单选、全选、反选、获得所有选中的checkbox