【问题标题】:jQuery Chosen: how to select 1 option value and remove the same one in another select-menu?jQuery Chosen:如何选择 1 个选项值并在另一个选择菜单中删除相同的选项值?
【发布时间】:2012-02-29 05:17:29
【问题描述】:

我已将 2 个元素放在一起。他们都在使用 jQuery Chosen 插件。

这是代码:

<div class="wrapper">
  <select data-placeholder="Number row 1" style="width:350px;" multiple class="chzn-select">
    <option value=""></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
  </select>

  <select data-placeholder="Number row 2" style="width:350px;" multiple class="chzn-select">
    <option value=""></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
  </select>
</div>

这是Javascript。 jQuery 库、最新的 Chosen 插件和 CSS 都包含在内。

<script>
$('.chzn-select').trigger("liszt:updated");
$('.chzn-select').chosen().change( function() {
  var selectedValue = $(this).find('option:selected').val();
  $(this).parent().find('option[value="'+ selectedValue +'"]:not(:selected)').attr('disabled','disabled');
});
</script>

这就是我想用上面的脚本完成的。

  • 有两个具有相同值的选择元素。例如,当在元素 1 中选择了值“1”时,我想在元素 2 中禁用它。
  • 但是。在我取消选择元素 1 中的值“1”后,它在元素 2 中仍然被禁用。这不是我想要的。取消选择第一个元素中的值后,其他值应该再次可用。

有人知道怎么做吗?

编辑

我现在已经在此处正确放置了一个 JSFiddle:http://jsfiddle.net/dq97z/3/。任何帮助将不胜感激!

【问题讨论】:

    标签: jquery select plugins option jquery-chosen


    【解决方案1】:

    应该这样做:

    http://jsfiddle.net/musicisair/dq97z/10/

    // cache selects for use later
    var selects = $('.chzn-select');
    
    // whenever the selection changes, either disable or enable the 
    // option in the other selects
    selects.chosen().change(function() {
        var selected = [];
    
        // add all selected options to the array in the first loop
        selects.find("option").each(function() {
            if (this.selected) {
                selected[this.value] = this;
            }
        })
    
        // then either disabled or enable them in the second loop:
        .each(function() {
    
            // if the current option is already selected in another select disable it.
            // otherwise, enable it.
            this.disabled = selected[this.value] && selected[this.value] !== this;
        });
    
        // trigger the change in the "chosen" selects
        selects.trigger("liszt:updated");
    });
    

    【讨论】:

    • 也不起作用。当我在 1 个菜单中选择值“2”时,它在另一个菜单中仍然可见。
    • 啊,我没有注意到您需要多选。 jsfiddle 重新上线后,我会尽快解决此问题。
    • 这太棒了。干净的编码和可理解的。竖起两个大拇指!
    • 你能解释一下这条线吗? this.disabled = selected[this.value] &amp;&amp; selected[this.value] !== this; 我知道您将选定的选项放在第一个循环中的数组中,在第二个循环中,如果值已经在该“选定”数组中,则禁用每个选项。但是&amp;&amp; selected[this.value] !== this 代表什么?
    • @DavidMurdoch 感谢脚本。对于更新版本,请使用 selects.trigger("chosen:updated");
    【解决方案2】:

    我正在尝试使用不带多项选择的代码,因此对于每个选择,只能选择一个选项。 如果在选择框中选择了一个选项,我想使用 allow_single_deselect: true 代码删除一个选项,但我无法让它工作。我禁用了搜索选项,因为我在此页面上不需要它。

    <script>//<![CDATA[ 
    $(function(){  
    $('.chzn-select').chosen({disable_search_threshold: 10}) .change(
    function() {
    var selectedValue = $(this).find('option:selected').val();
    $(this).parent().parent().find('option[value="'+ selectedValue +'"]:not(:selected)').attr('disabled','disabled');
    $('.chzn-select').trigger("liszt:updated");
    });
    
    });//]]></script>       
    

    【讨论】:

      【解决方案3】:

      由于您正在修改在第一个下拉列表中选择的选项,因此之前被禁用的选项不会再次启用。

      您需要先启用所有选项,然后仅禁用选定的选项。试试这个

      $('.chzn-select').trigger("liszt:updated");
      $('.chzn-select').chosen().change( function() {
          var selectedValue = $(this).find('option:selected').val();
          var $options = $(this).parent().find('option').attr('disabled', false);
          $options.filter('option[value="'+ selectedValue +'"]:not(:selected)')
          .attr('disabled', true);
      });
      

      【讨论】:

      • 我已经清空了缓存并硬刷新了页面,但这种方法也不起作用。例如,我想在第一个菜单中选择值“3”,然后立即禁用第二个菜单中的值“3”。
      【解决方案4】:

      你在每次更改时都设置了禁用,当然它不会起作用...

      在这段代码中,我根据之前的值更改了 disabled 属性;

      <script>
      $('.chzn-select').trigger("liszt:updated");
      $('.chzn-select').chosen().change( function() {
        var selectedValue = $(this).find('option:selected').val();
        $(this).parent().find('option[value="'+ selectedValue +'"]:not(:selected)')
              .prop("disabled", function( i, val ) {
                    return !val;
                  });
      });
      </script>
      

      【讨论】:

      • 根本不起作用。当我选择值“1”时,另一个选择菜单中的另一个值“1”仍然可用。
      • 正确的JSFiddle放在这里:很抱歉给您带来不便。 jsfiddle.net/dq97z/3
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多