【问题标题】:Chosen select - filter <optgroup > using jquery选择选择 - 使用 jquery 过滤 <optgroup>
【发布时间】:2019-12-12 18:57:57
【问题描述】:

我在 asp.net 中使用选择的下拉菜单。有没有办法过滤 optgroup ?

有一个单选按钮列表控件,其值为 US 和 UK。我想根据用户的单选按钮选择过滤下拉列表。如果选择了“US”,则下拉菜单应仅显示 US optgroup 记录。

这里是 jsfiddle;

http://jsfiddle.net/7ngftsq2/

这是我到目前为止没有运气的尝试;

('#rbDistDiv input').change(function () {
            // The one that fires the event is always the
            // checked one; you don't need to test for this
            var ddlDist = $('#VCSdistSelect_ddlDist'), 
            val = $(this).val(),
            region = $('option:selected', this).text();

            $('span > optgroup', ddlDist).unwrap();
            if (val !== '%') {
                $('optgroup:not([label="' + region + '"])', ddlDist).wrap('<span/>');
            }


        });

【问题讨论】:

  • 你能粘贴渲染的单选按钮的 HTML 吗?
  • 我已将其添加到我的问题中。
  • 您将下拉菜单呈现为 &lt;ul&gt; ?不是&lt;select&gt;
  • 我使用 ListItem 将其绑定在代码后面,然后将其包装 $("select#dist_ddlDist option[division='UKM']").wrapAll("" );
  • 你也能发一下吗?你能发布尽可能多的渲染 HTML 吗?

标签: javascript jquery asp.net jquery-chosen


【解决方案1】:

你应该可以做类似这样的事情:

$(".chosen").chosen({
  width: '600px',
  allow_single_deselect: true,
  search_contains: true
});
 
let UK_optgroup = $("#optgroup-uk").clone();
let US_optgroup = $("#optgroup-us").clone();
let CA_optgroup = $("#optgroup-ca").clone();

function filterSelect(selectedRadio) {
  switch(selectedRadio) {
    case "US": return US_optgroup
    case "UK": return UK_optgroup
    case "CA": return CA_optgroup
  }
}
 
$('[type="radio"]').on('change', function() {
  // This is only here to clear the selected item that is shown on screen
  // after a selection is made. You can remove this if you like.
  $("#selected-item").html("");
  // Everything from here down is needed.
  $("#select")
    .children()
    .remove('optgroup');
  $("#select")
    .append(filterSelect(this.value))
    .trigger("chosen:updated");
});

$("#select").on('change', function(event) {
  $("#selected-item").html("Selected Item: <b>" + event.target.value + "</b>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.css" rel="stylesheet"/>

<table id="rbDistDiv" border="0">
  <tbody>
    <tr>
      <td><input id="rbDistDiv_0" type="radio" name="rbDistDiv" value="US"><label for="rbDistDiv_0">US</label></td>
    </tr>
    <tr>
      <td><input id="rbDistDiv_1" type="radio" name="rbDistDiv" value="UK"><label for="rbDistDiv_1">UK</label></td>
    </tr>
    <tr>
      <td><input id="rbDistDiv_2" type="radio" name="rbDistDiv" value="CA"><label for="rbDistDiv_2">CA</label></td>
    </tr>    
  </tbody>
</table>
<p id="selected-item"></p>
<select id="select" class="chosen">
  <option value="" disabled selected>Select your option</option>
  <optgroup id="optgroup-uk" label="UK">
    <option value="Adrian">Adrian</option>
    <option value="Alan">Alan</option>
    <option value="Alan B">Alan B</option>
  </optgroup>
  <optgroup id="optgroup-us" label="US">
    <option value="John">John</option>
    <option value="Billy">Billy</option>
    <option value="Chris">Chris</option>
  </optgroup>  
  <optgroup id="optgroup-ca" label="CA">
    <option value="Gabrielle">Gabrielle</option>
    <option value="Maude">Maude</option>
    <option value="Morgan">Morgan</option>
  </optgroup>    
</select>

【讨论】:

  • 我已经简化了我的问题。请检查一下。
  • 不工作。这是最新的 jsfiddle.net/7ngftsq2
  • 你是什么意思?..这看起来不像我的代码..我认为你的意思是回应另一个答案。
  • 看起来chosen 处理这个真的很奇怪......我已经更新了我的答案来展示你如何做到这一点。
  • @user1263981 是您要找的吗?
【解决方案2】:

当对单选按钮进行更改时,只需通过标签隐藏/显示它们。

$('[type="radio"]').on('change', function () {
  $('select')
    .val('')  // reset value if selection already made
    .find('optgroup')  // find the groups
      .hide()  // hide them all 
    .filter('[label="' + this.value + '"]')  // find the one that matches radio
      .show() // show it

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="rbDistDiv" border="0">
        <tbody><tr>
            <td><input id="rbDistDiv_0" type="radio" name="rbDistDiv" value="US"><label for="rbDistDiv_0">US</label></td>
        </tr><tr>
            <td><input id="rbDistDiv_1" type="radio" name="rbDistDiv" value="UK"><label for="rbDistDiv_1">UK</label></td>
        </tr>
    </tbody></table>
    
    
    <br>

<select name="VCSdistSelect$ddlDist" id="VCSdistSelect_ddlDist" class="chosen-select" data-placeholder="Select a Distributor" >
	<option selected="selected" value=""></option>
		<optgroup label="US">
			<option value="123" division="US">Aaron</option>
		</optgroup>
	<optgroup label="UK">
		<option value="655" division="UK">John</option>
	</optgroup>
</select>

【讨论】:

  • 不工作。这是最新的jsfiddle.net/7ngftsq2
  • 您的 HTML 无效,您的选择中不能有跨度。有趣的是,您正在使用其他一些可能搞砸的插件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
相关资源
最近更新 更多