【问题标题】:jquery multiselect - nested <option>jquery multiselect - 嵌套 <option>
【发布时间】:2012-02-05 14:28:43
【问题描述】:

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

如何在此处实现嵌套&lt;option&gt;?我知道这是可能的,因为呈现的 html 使用 &lt;li&gt; 标签

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

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

有没有人知道如何实现这种解决方案。任何帮助将不胜感激。

【问题讨论】:

  • 你想对嵌套的&lt;option&gt;说什么?选项组?缩进选项?
  • 我认为他想要的是当用户选择伦敦时,它也会自动选择英格兰。
  • 意味着您希望如果您检查england,那么应该选择英国下的所有地区。我说得对吗??

标签: jquery select option


【解决方案1】:

说明

假设我了解您想要什么,您可以使用 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:是的,这就是我需要的。我将不得不添加一些 CSS 让它看起来很漂亮。您可以附加您的答案,我将非常乐意接受它

您可以在这个 jSFiddle 中看到结果。

我鼓励您下载资源(javascript 和 css 文件)以将其放在您的环境中。

完整的工作示例

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

【讨论】:

  • 但我的 optgroup 中也需要一个复选框,所以在“England”和“USA”旁边
  • 如果您点击“英格兰”,项目也会被检查
  • 我知道,但我需要在那里有“复选框”控件。所以当我点击“英格兰”旁边的复选框时,所有的孩子也会被选中。
  • 这不可能的问题是,在检查“英格兰”之后,所有的孩子也会被选中,到目前为止还不错。但是,如果您将表单发送到例如服务器,您将获得所有检查结果,这是没有意义的。如果你愿意,我会为你创建一个插件,但这不是你应该处理的方式。
  • 情况是这些国家/地区有一个特定的 ID,我需要这些 ID 才能将其与第三方网络服务一起使用。不幸的是,此服务的创建方式是,使用英格兰 ID 调用 Find 方法将返回与使用英格兰地区 ID 调用方法不同的结果。因此,我需要在 中有这样的组合框。你能为我创建这样的插件吗?
【解决方案2】:

您可以使用optgroup 来达到这个效果,在这种情况下,当单击 optgroup(例如“England”)时,它下面的所有选项都会被选中。见a demo here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 2020-01-13
    • 2019-12-28
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多