我想从之前 Select 的下拉列表中隐藏选项,但我的解决方案只适用于两个下拉列表.

这意味着当我进入第三个下拉列表时,它不会显示第二个下拉列表中的选定选项,这是可以的,但它会显示第一个下拉列表中的选定选项.

所以,据我所知,我使用的方法覆盖了最后一个,这就是为什么它不起作用.

以下是我的 Select 列表:

<select id="select1" 
        onchange="getSelectValue(this.value)" 
        asp-for="AssignedGroups.GroupMemberId1" 
        asp-items="@(new SelectList(Model.Agents,"Agent_Id","Agent_Id"))" 
        class="form-control">
    <option hidden selected></option>
</select>

<select id="select2" 
        onchange="getSelectValue2(this.value)" asp-
        for="AssignedGroups.GroupMemberId2"  
        asp-items="@(new SelectList(Model.Agents,"Agent_Id","Agent_Id"))"  
        class="form-control" >
    <option hidden selected></option>
</select>

<select id="select3" 
        onchange="getSelectValue3(this.value)" asp-
        for="AssignedGroups.GroupMemberId3" 
        asp-items="@(new SelectList(Model.Agents, "Agent_Id", "Agent_Id"))" 
        class="form-control">
    <option hidden selected></option>
</select>

还有我的 playbook :

function getSelectValue(select1) {
            $("#select2 option[value='" + select1 + "']").hide();
            $("#select2 option[value!='" + select1 + "']").show();
            $("#select3 option[value='" + select1 + "']").hide();
            $("#select3 option[value!='" + select1 + "']").show();

    }

    function getSelectValue2(select2) {
        
            $("#select1 option[value='" + select2 + "']").hide();
            $("#select1 option[value!='" + select2 + "']").show();
            $("#select3 option[value='" + select2 + "']").hide();
            $("#select3 option[value!='" + select2 + "']").show();

    }

    function getSelectValue3(select3) {
        $("#select1 option[value='" + select3 + "']").hide();
        $("#select1 option[value!='" + select3 + "']").show();
        $("#select2 option[value='" + select3 + "']").hide();
        $("#select2 option[value!='" + select3 + "']").show();

        
    }

推荐答案

我创建了一个函数来处理所有选项的取消隐藏和隐藏,然后根据所有选定的值过滤其他下拉列表的选项.以下是功能/演示:

function hideOthers() {
  // Get all selected values
  let selectedValues = $(".form-control option:selected").map(function() {
    if (this.value.length === 0) {
      return null;
    }
    return this.value;
  });
  // Unhide all so we can hide the correct ones
  $("select.form-control option").removeAttr('hidden');

  // Filter out the selected values from dropdowns
  $(".form-control").each(function() {
    var selectElem = $(this);
    $.each(selectedValues, function(index, value) {
      // If the selected value from the array is from the applicable <select>, skip hiding
      if (selectElem.find("option:selected").val() !== value) {
        selectElem.find(`option[value="${value}"]`).attr('hidden', true);
      }
    });
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select1" onchange="hideOthers()" class="form-control">
  <option hidden selected></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>
</select>

<select id="select2" onchange="hideOthers()" class="form-control">
  <option hidden selected></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>
</select>

<select id="select3" onchange="hideOthers()" class="form-control">
  <option hidden selected></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>
</select>

Jquery相关问答推荐

无法通过单击从元素获取数据

jQuery:使用变量作为 Select 器

在 CSS3 或 jQuery 中将元素的宽度从 0% 设置为 100% 动画,它和它的包装器只需要它们需要的宽度,没有预先设置的宽度

.trigger() 与 .click() 中的 jQuery 优势/差异

JQuery手风琴自动高度问题

jQuery绑定到粘贴事件,如何获取粘贴的内容

如何在 Backbone.js - Backbone.sync 或 jQuery.ajax 中保存整个集合?

在不使用提交按钮的情况下触发标准 HTML5 验证(表单)?

当ID包含方括号时按ID查找DOM元素?

删除索引后的所有项目

jQuery:value.attr 不是函数

如何将文本从 div 复制到剪贴板

将打开/关闭图标添加到 Twitter Bootstrap 可折叠项(手风琴)

jquery: $(window).scrollTop() 但没有 $(window).scrollBottom()

如何从所有元素jquery中删除类

如何在页面加载时启动 jQuery Fancybox?

javascript jquery单选按钮单击

jQuery位置href

将多个参数传递给 jQuery ajax 调用

jQuery:通过 .attr() 添加两个属性;方法