【问题标题】:Remove values from drop down lists从下拉列表中删除值
【发布时间】:2014-07-22 10:58:49
【问题描述】:

目前,我有两个下拉列表,其中填充了用户的名字和姓氏。当从第一个下拉列表中选择用户时,该名称在第二个下拉列表中不可用。

我想添加第三个下拉列表,使第一个和第二个列表中选择的值不可用。如何修改我当前的代码以支持此功能?

当前代码可以在这里找到:http://jsfiddle.net/NfTNA/

    function removeOptions(selectA,selectB,selectC) {
    var firstValue = $(selectA).children(":selected").attr("value");
    var secondValue = $(selectB).children(":selected").attr("value");
    var thirdValue = $(selectC).children(":selected").attr("value");
    // get the other element from the hidden select to put back
    var prior = $("#hiddenContainer").children("[value!="+secondValue+"]").data("prior");
    if (prior != undefined) {
        $("#hiddenContainer").children("[value!=" + secondValue + "]").insertAfter($(selectB).children("[value=" + prior.prior + "]"));
    }
    if (firstValue != 0) {
        // add the prior id data to the element before removing it
        var priorValue = $(selectB).children("[value="+firstValue+"]").prev().attr("value");
        $(selectB).children("[value="+firstValue+"]").data("prior",{prior:priorValue});

        // move the selected element of selectA in selectB to hidden select 
        $(selectB).children("[value="+firstValue+"]").appendTo("#hiddenContainer");
    }
    // reselect the option in the secondary select
    $(selectB).val(secondValue);

}

提前致谢。

【问题讨论】:

  • 澄清一下 - 第一个和第二个下拉列表有名称列表。当您从第一个列表中选择一个名称时,它会从第二个列表中删除。第三个列表会发生什么?
  • 第一个和第二个列表中选择的名称将不会出现在第三个列表中。
  • 我的另一个问题可以被视为 glossrob 问题的附加问题 - 如果用户首先选择第三个下拉菜单或第二个下拉菜单会发生什么?还是只有当用户在第一个下拉菜单上进行选择时才启用第二个下拉菜单?
  • 目前,选择的顺序无关紧要。如果从第二个下拉列表中选择了 userA,则 userA 在第一个下拉列表中不可用。

标签: jquery


【解决方案1】:

看看这个小提琴http://jsfiddle.net/YHjuz/1/
它会给你进一步的想法。
仅供参考,您不能在 IE7 中隐藏下拉列表的选项。

【讨论】:

  • 这也是一个很好的例子。公认的解决方案正是我想要的;但是,我会记住您的建议和建议。谢谢。
【解决方案2】:

用 jQuery 来做这件事(见http://jsfiddle.net/NfTNA/38/

示例标记

<label for="reviewer">Select 1<sup>st</sup> Reviewer:</label>
<br />
<select name="optionsA" id="optionsA">
    <option value="">&ndash; select &ndash;</option>
    <option value="jsmith">Smith, John (jsmith)</option>
    <option value="bwright">Wright, Bob (bwright)</option>
</select>
<br />
<label for="reviewer">Select 2<sup>nd</sup> Reviewer:</label>
<br/>
<select name="optionsB" id="optionsB">
    <option value="">&ndash; select &ndash;</option>
    <option value="jsmith">Smith, John (jsmith)</option>
    <option value="bwright">Wright, Bob (bwright)</option>
</select>
<br />
<label for="reviewer">Select 3<sup>rd</sup> Reviewer:</label>
<br/>
<select name="optionsC" id="optionsC">
    <option value="">&ndash; select &ndash;</option>
    <option value="jsmith">Smith, John (jsmith)</option>
    <option value="bwright">Wright, Bob (bwright)</option>
</select>

jQuery

var firstSelected = null;
var secondSelected = null;

//initally order select lists (this is a bit of cheat, to help later on

$('#optionsA').change(function() {
    //remove the currently selected option from 2nd/3rd drop down
    var selectedValue = $('#optionsA option:selected')
    if (selectedValue.val() != "") {
        $('#optionsB option[value="' + selectedValue.val() + '"]').remove();
        $('#optionsC option[value="' + selectedValue.val() + '"]').remove();
    }
    else {
        selectedValue = null;
    }

    //add previous item back in to 2nd/3rd drop down
    if (firstSelected != null) {
        $('#optionsB').append($('<option>', { value : firstSelected.val() }).text(firstSelected.text())); 
        $('#optionsC').append($('<option>', { value : firstSelected.val() }).text(firstSelected.text())); 
    }

    //save the currently selected value
    firstSelected = selectedValue;
});

$('#optionsB').change(function() {
    //remove the currently selected option from 1st/3rd drop down
    var selectedValue = $('#optionsB option:selected')
    if (selectedValue.val() != "") {
        $('#optionsA option[value="' + selectedValue.val() + '"]').remove();
        $('#optionsC option[value="' + selectedValue.val() + '"]').remove();
    }
    else {
        selectedValue = null;
    }

    //add previous item back in to 1st/3rd drop down
    if (secondSelected != null) {
        $('#optionsA').append($('<option>', { value : secondSelected.val() }).text(secondSelected.text())); 
        $('#optionsC').append($('<option>', { value : firstSelected.val() }).text(firstSelected.text())); 
    }

    //save the currently selected value
    secondSelected = selectedValue;
});

这是基于以下几点:

  • 从选择列表 1 中选择一个选项,从选择列表 2 和 3 中删除选项

  • 从选择列表 2 中选择一个选项,从选择列表 1 和 3 中删除选项

  • 之前选择的选项被重新添加到列表中

【讨论】:

  • 如果第二个下拉列表中的值发生更改,他可能希望返回该值..
  • 正确,当前代码按要求工作,只需要附加它以允许第三个相关下拉列表。
  • 在我的辩护中,这是一个非常重要的要求!您的 jsFiddle 仅显示 2 个下拉菜单?这和你的问题有什么关系?
  • 在当前状态下,我的代码使用两个下拉列表。我正在寻求帮助以添加第三个列表,因此没有。您上面的代码完全符合我的要求,并且比我的原始代码更苗条。感谢您的帮助和理解!
【解决方案3】:

这可能过于简单,但我看到您似乎在更改最后一个下拉列表时试图保留所选值并删除所有其他值,那么这样做怎么样:

$(document).ready(function() {
    $('#optionsC').change(function() {
        $('#selectA').find('option:not(:selected)').remove();
        $('#selectB').find('option:not(:selected)').remove();
    });
});

或者,如果您尝试从框 A 和 B 中删除第三个框中的名称:

$('#optionsC').change(function() {
    var selectedValue = $(this).val();
    if (selectedValue != "") {
        $('#optionsA option[value="' + selectedValue + '"]').remove();
        $('#optionsB option[value="' + selectedValue + '"]').remove();
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多