微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery multiselect – 嵌套

我正在使用以下 jquery插件在我的select控件中选择多个选项: jquery multiselect

如何实现嵌套<选项>这里?我知道这是可能的,因为渲染的html使用< li>标签

情况是我想在我的组合框中得到类似的结果:

[ ] England
   [ ] London
   [ ] Leeds
   [ ] Manchaster

有谁知道如何实现这种解决方案.任何帮助都会很有帮助.

解决方法

描述

假设我明白你想要什么,你可以使用optgroup来做到这一点.

看看我为你创造的这个jsFiddle Demonstration.

样品

<select multiple="multiple" size="5">
<optgroup label="England">
    <option value="London">London</option>
    <option value="Leeds">Leeds</option>
    <option value="option3">Manchaster</option>
</optgroup>
<optgroup label="USA">
    <option value="option4">New York</option>
    <option value="option5">Chicago</option>
</optgroup>
</select>

更多信息

> jsFiddle Demonstration
> jQuery UI MultiSelect Widget
> jQuery UI MultiSelect Widget Demos
> vZHai – jQuery UI MultiSelect Widget

更新

我创建了一个jsFiddle Demonstration.

完整的工作样本

<!DOCTYPE html>
<html>
<head>
  <Meta http-equiv="content-type" content="text/html; charset=UTF-8">

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
  <link rel="stylesheet" type="text/css" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/jquery.multiselect.css">
  <link rel="stylesheet" type="text/css" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/assets/style.css">
  <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css">
  <script type='text/javascript' src="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/src/jquery.multiselect.js"></script>

  <script type='text/javascript'>//<![CDATA[ 
    $(function(){
        $("select").multiselect();
    });  
  </script>
</head>
<body>
  <select multiple="multiple" size="5">
    <optgroup label="England">
        <option value="London">London</option>
        <option value="Leeds">Leeds</option>
        <option value="option3">Manchaster</option>
    </optgroup>
    <optgroup label="USA">
        <option value="option4">New York</option>
        <option value="option5">Chicago</option>
    </optgroup>
  </select>
</body>
</html>

经过与niao的深入讨论后更新

niao: yes,that’s what I need. I will have to add some css to make it looks pretty. You can append your answer and I will be more than happy to accept it

您可以在此jSFiddle中查看结果.

I encourage you to download the resources (javascript and css files) to put that on your enviroment.

完整的工作样本

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <Meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

  <script src="http://wwwendt.de/tech/dynatree/jquery/jquery.js" type="text/javascript"></script>
  <script src="http://wwwendt.de/tech/dynatree/jquery/jquery-ui.custom.js" type="text/javascript"></script>
  <script src="http://wwwendt.de/tech/dynatree/jquery/jquery.cookie.js" type="text/javascript"></script>

  <link href="http://wwwendt.de/tech/dynatree/src/skin/ui.dynatree.css" rel="stylesheet" type="text/css" id="skinSheet">
  <script src="http://wwwendt.de/tech/dynatree/src/jquery.dynatree.js" type="text/javascript"></script>

  <script type="text/javascript">
    var treeData = [
      {title: "England",key: "England",expand: true,children: [
          {title: "Region",key: "Region",activate: true,expand:true,children: [
              {title: "London",key: "London" },{title: "Leeds",key: "Leeds" }
            ]
          }
        ]
      }
    ];
    $(function(){
      $("#tree3").dynatree({
        checkBox: true,selectMode: 3,children: treeData,onSelect: function(select,node) {
          // Get a list of all selected nodes,and convert to a key array:
          var selKeys = $.map(node.tree.getSelectednodes(),function(node){
            return node.data.key;
          });
          $("#displayText").val(selKeys.join(","));
        },onDblClick: function(node,event) {
          node.toggleSelect();
        },onKeydown: function(node,event) {
          if( event.which == 32 ) {
            node.toggleSelect();
            return false;
          }
        },});

      $("#opener").click(function() {
         var tree = $("#tree3");
         if (tree.css("display") == "none")
         {
            tree.css("display","block") 
         } else {
            tree.css("display","none");
         }
      });
  });
</script>
</head>

<body class="example">
  <div style="width: 500px;">
      <div>
        <input readonly="true" type="text" id="displayText" style="float:left;width:470px"/>
        <input type="button" id="opener" style="width:1px"/>
      </div>
      <div style="display:none" id="tree3"></div>
  </div>
</body>
</html>

原文地址:https://www.jb51.cc/jquery/175749.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐