jquery 多重选择 - 嵌套 <选项>

标签 jquery html-select

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

如何实现嵌套<option>这里?我知道这是可能的,因为渲染的 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 .

完整的工作示例

<!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>

关于jquery 多重选择 - 嵌套 <选项>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781905/

相关文章:

jquery - 如何让 Controller 记住ajax post数据Yii2

javascript - jquery 多个复选框启用文本字段

jquery - jquery中textarea的字符数限制

php - 如何将动态下拉列表中的值保存到 MySQL 数据库中

jquery - 使用 javascript 更改 css

javascript - 如何在 jQuery 中删除当前窗口中的所有框架?

javascript - playframework 选择帮助器过滤选项或基于另一个选择的设置选项

javascript - React警告: flattenChildren(. ..):遇到两个拥有相同 key 的 child

javascript - 动态更新 JavaScript 和 PHP 中的选择元素选项

javascript - 使用选择标签和多个 Canvas 使用 Chart.js 创建下拉选项