简体   繁体   English

如何使用其他下拉菜单中未选择的选项填充下拉菜单?

[英]How to populate a dropdown using unselected options from other dropdowns?

I use JQuery Steps.In 1st and 2nd step, i have a dropdown with multiple select option. 我使用JQuery Steps.In第一步和第二步,我有多个选择选项的下拉列表。

<select name="numbers" id="numbers">
  <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>

in step2(same as step1 but with diff. purpose) 在步骤2中(与步骤1相同,但有区别的目的)

<select name="allocate_numbers" id="allocate_numbers">
  <option value="1">1</option>
   : : :
 </select>

now i have a 3rd dropdown in 3rd step.Here i want selectbox like..suppose user select 1,2 in step1, 3,4 in step2. 现在我有3 step.Here我想选择框like..suppose用户选择第三个下拉1,2在步骤1中, 3,4在第二步。

now,i want selectbox that contain only 5,6 in step3. 现在,我想在第3步中仅包含5,6框。
How to do that? 怎么做?

side note: I done validation to prevent duplicate entry for step1 and step2 dropdown. 旁注:我做了验证,以防止重复输入步骤1和步骤2下拉菜单。 means if user select 1,2 in step1 then he can not select 1,2 in step2. 表示如果用户在步骤1中选择1,2,则不能在步骤2中选择1,2。 I don't find suitable title for my quetion so feel free to edit it. 我找不到适合自己问题的标题,请随时对其进行编辑。

Updated and better answer: 更新和更好的答案:

The solution turns out to be rather simple. 结果证明是相当简单的。 Find all unselected options in first dropdown and filter those that are not selected in other dropdowns. 在第一个下拉菜单中找到所有未选择的选项,并过滤其他下拉菜单中未选择的选项。 Add event delegation and the result is: 添加事件委托,结果为:

$(document).on("change", ".numbers", function () {
    $("#select1").empty();
    $(".numbers:eq(0) option:not(:selected)").filter(function () {
        return $(".numbers:gt(0) option[value=" + this.value + "]:selected").length === 0
    }).clone().appendTo("#select1");
});

Demo here 在这里演示

Original answer: 原始答案:

This turned out to be a real brain twister. 原来这是一个真正的大脑扭曲者。 Basically you need to: 基本上,您需要:

  1. Prepare combined list of selected values 准备所选值的合并列表
  2. Iterate over options from both lists 遍历两个列表中的选项
  3. Filter options that are not selected 筛选未选择的选项
  4. Filter duplicates 过滤重复项
  5. Clone and append 克隆并追加

Here is my go at it but there is probably a simpler solution: 这是我的努力,但可能有一个更简单的解决方案:

$("#numbers, #allocate_numbers").on("change", function () {
    $("#select3").empty();
    var selectedValues = [],
        renderedValues = [];
    $.merge(selectedValues, $("#numbers").val() || []);
    $.merge(selectedValues, $("#allocate_numbers").val() || []);
    $("#numbers option, #allocate_numbers option").filter(function () {
        if ($.inArray(this.value, selectedValues) === -1) {
            if ($.inArray(this.value, renderedValues) === -1) {
                renderedValues.push(this.value);
                return true;
            }
        }
        return false;
    }).clone().appendTo("#select3");
});

Demo here 在这里演示

Initially, keep both dropdown values in 3rd dropdown and remove the values from 3rd, when it is selected in first and second dropdown. 最初,将两个下拉列表中的值都保留在第3个下拉列表中,然后在第一个和第二个下拉列表中将其选中时,将其从第3个下拉列表中删除。 This is the easiest method. 这是最简单的方法。

Try jQuery code to remove and add options in dropdown. 尝试使用jQuery代码在下拉菜单中删除和添加选项。

If one of the dropdown is dynamically added then try this: 如果下拉列表之一是动态添加的,请尝试以下操作:

    $(document).on('change','.numbers, #allocate_numbers',function(){
    $("#select3").empty();
    var selectedValues = [],
        renderedValues = [];
            $('.numbers option:selected').each(function() {
              $.merge(selectedValues, $(this).val() || []);
            });
    $.merge(selectedValues, $("#allocate_numbers").val() || []);
    $(".numbers option, #allocate_numbers option").filter(function () {
        if ($.inArray(this.value, selectedValues) === -1) {
            if ($.inArray(this.value, renderedValues) === -1) {
                renderedValues.push(this.value);
                return true;
            }
        }
        return false;
    }).clone().appendTo("#select3");
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下拉选项从其他下拉菜单中删除 - Dropdown Options remove from other dropdowns 具有相同选项的多个下拉菜单:如何从其他下拉菜单中删除一个下拉列表的当前选定项目? - Multiple dropdowns with same options: how to remove currently selected item of one dropdown from other dropdowns? 使用jQuery在mulitiselect下拉列表中禁用optgroup中未选择的选项 - Disable unselected options from optgroup in mulitiselect dropdown using jquery 如何基于另一个下拉列表中的选定值填充其他下拉列表中的值 - How to populate values in other Dropdowns based on selected values in another Dropdown 如何使用 promise 动态填充下拉选项 - How to populate dropdown options dynamically using a promise JSP:如何将下拉列表的所有(选中和未选中)选项传递给 servlet? - JSP: How to pass all (selected and unselected) options of a dropdown list to servlet? 动态添加下拉列表,并基于一个下拉列表中的选择添加其他列表中的选项 - Adding dropdowns dynamically and based on selection in one dropdown select options in other 使用JavaScript从PHP数组中填充带有选项的动态创建的下拉列表 - Populate Dynamically Created Dropdown with Options from PHP array using javascript 结合使用实体模型和AngularJS从下拉列表中填充单选选项 - Using Entity Model with AngularJS to populate radio options from dropdown React - 如何通过将状态作为道具传递,基于从另一个下拉列表中的选择来填充一个下拉列表 - React - How to Populate one Dropdowns based on selection from another Dropdown by Passing State as props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM