冻_结 2015-07-19 09:57 采纳率: 100%
浏览 3515
已采纳

使用Jquery ztree 在同一个页面中出现多个input选择,每个input数据不同

使用一下dome代码,怎么在同一个页面中出现多个input选择,每个input下拉列表中的数据是不通的。

 <!DOCTYPE html>
<HTML>
<HEAD>
    <TITLE> ZTREE DEMO - radio 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-3.5.js"></script>
    <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
<!--    <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>-->
    <SCRIPT type="text/javascript">
        <!--
        var setting = {
            check: {
                enable: true,
                chkStyle: "radio",
                radioType: "all"
            },
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: onClick,
                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 onClick(e, 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>带 radio 的单选下拉菜单  -- zTree</h1>
<h6>[ 文件路径: super/select_menu_radio.html ]</h6>
<div class="content_wrap">
    <div class="zTreeDemoBackground left">
        <ul class="list">
            <li class="title">&nbsp;&nbsp;<span class="highlight_red">勾选 radio 或者 点击节点 进行选择</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>用 radio 也可以轻松实现单选的下拉菜单</li>
                <li>单选其实没有必要使用 radio, 此 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>
  • 写回答

2条回答 默认 最新

  • 斯洛文尼亚旅游 2015-07-21 01:12
    关注
             function onClick(e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(currentTree);////////////
                zTree.checkNode(treeNode, !treeNode.checked, null, true);
                return false;
            }
    
    
            function hideMenu() {
                $("div[id='menuContent']").fadeOut("fast");/////////////id重复了,只能用属性选择器
                $("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);
                $.fn.zTree.init($("#treeDemo1"), setting, zNodes1);
            });
    
    
             function onCheck(e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(currentTree),//////////////
                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");
                currentObj.attr("value", v);////////使用变量存储的input对象
            }
             var currentObj;////////////存储当前操作的input对象
             var currentTree;//记住当前用的那棵树
            function showMenu(o,treeId) {
                var cityObj = $(o);
                if (o.tagName == 'A') cityObj = cityObj.parent().find('input');//点击的是连接,获取和连接对应的input对象
                currentObj = cityObj///
                currentTree=treeId////
                var cityOffset = cityObj.offset();
                //
                $("#"+currentTree).parent().css({ left: cityOffset.left + "px", top: cityOffset.top + cityObj.outerHeight() + "px" }).slideDown("fast");
    
                var treeObj = $.fn.zTree.getZTreeObj(currentTree);
                var nodes = treeObj.getSelectedNodes();
                if (nodes.length > 0) treeObj.checkNode(nodes[0], false, null, false);//取消ztree的选择
    
                $("body").bind("mousedown", onBodyDown);
            }
    
                 <ul class="list">
                    <li class="title">&nbsp;&nbsp;<span class="highlight_red">勾选 radio 或者 点击节点 进行选择</span></li>
                    <li class="title">
                        &nbsp;&nbsp;Test: <input id="citySel" type="text" readonly value="" style="width:120px;" onclick="showMenu(this,'treeDemo');" />
                        &nbsp;<a id="menuBtn" href="#" onclick="showMenu(this, 'treeDemo'); return false;">select</a>
                    </li>
                    <li class="title"><!--使用第二棵树-->
                        &nbsp;&nbsp;Test: <input id="citySel" type="text" readonly value="" style="width:120px;" onclick="showMenu(this, 'treeDemo1');" />
                        &nbsp;<a id="menuBtn" href="#" onclick="showMenu(this, 'treeDemo1'); return false;">select</a>
                    </li>
                    <li class="title">
                        &nbsp;&nbsp;Test: <input id="citySel" type="text" readonly value="" style="width:120px;" onclick="showMenu(this, 'treeDemo');" />
                        &nbsp;<a id="menuBtn" href="#" onclick="showMenu(this, 'treeDemo'); return false;">select</a>
                    </li>
                </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。