简体   繁体   English

在下拉菜单1中选中后,从下拉菜单2中隐藏选项

[英]Hide option from dropdown 2 when selected in dropdown 1

I have two dropdowns, both have the same items in them. 我有两个下拉菜单,其中都包含相同的项目。 If an option is selected in dropdown 1 then I would like to hide that option in dropdown 2. When it is unselected in dropdown 1 I would like it to appear again in dropdown 2 and whichever option is then selected to then be hidden in dropdown 2. I am trying to have this exclude the blank option in the first index. 如果在下拉列表1中选择了一个选项,那么我想在下拉列表2中将该选项隐藏。当在下拉列表1中取消选择该选项时,我希望它再次出现在下拉列表2中,然后选择其中一个选项,然后将其隐藏在下拉列表2中我试图让它在第一个索引中排除空白选项。

Here is a codepen that I started, but I am not sure where to go from here: 这是我开始的一个codepen,但是我不确定从这里去哪里:

http://codepen.io/cavanflynn/pen/EjreJK http://codepen.io/cavanflynn/pen/EjreJK

    var $dropdown1 = $("select[name='dropdown1']");
    var $dropdown2 = $("select[name='dropdown2']");

    $dropdown1.change(function () {
        var selectedItem = $($dropdown1).find("option:selected").val;
});

Thanks for your help! 谢谢你的帮助!

As said in comments, one of the options is to disable/enable options according to the selection in the first select, like below. 如评论中所述,选项之一是根据第一个选择中的选择禁用/启用选项,如下所示。 This would work on all browsers as opposed to hide/show which doesn't. 这将适用于所有浏览器,而不是隐藏/显示。

 var $dropdown1 = $("select[name='dropdown1']"); var $dropdown2 = $("select[name='dropdown2']"); $dropdown1.change(function() { $dropdown2.find('option').prop("disabled", false); var selectedItem = $(this).val(); if (selectedItem) { $dropdown2.find('option[value="' + selectedItem + '"]').prop("disabled", true); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="dropdown1"> <option></option> <option value="1">Test 1</option> <option value="2">Test 2</option> <option value="3">Test 3</option> </select> <select name="dropdown2"> <option></option> <option value="1">Test 1</option> <option value="2">Test 2</option> <option value="3">Test 3</option> </select> 

Another option is to remove/add options in the 2nd dropdown based on the selection in the first via .clone() , as below. 另一个选项是根据第一个通过.clone()的选择在第二个下拉菜单中删除/添加选项,如下所示。

var $dropdown1 = $("select[name='dropdown1']");
var $dropdown2 = $("select[name='dropdown2']");

$dropdown1.change(function() {
    $dropdown2.empty().append($dropdown1.find('option').clone());
    var selectedItem = $(this).val();
    if (selectedItem) {
        $dropdown2.find('option[value="' + selectedItem + '"]').remove();
    }
});

A Demo 演示

You should use this plugin http://gregfranko.com/jquery.selectBoxIt.js 您应该使用此插件http://gregfranko.com/jquery.selectBoxIt.js

It has some really nice callbacks and customisation options. 它具有一些非常好的回调和自定义选项。

When one changes you can put it in the callback to update the other. 当一个更改时,您可以将其放在回调中以更新另一个。

Here is one way of doing it. 这是一种方法。 You do need to include jQuery, and then as long as the value isn't empty hide the option with the similar value. 您确实需要包括jQuery,然后只要该值不为空,就可以隐藏具有相似值的选项。

 var $dropdown1 = $("select[name='dropdown1']"); var $dropdown2 = $("select[name='dropdown2']"); $dropdown1.change(function() { $dropdown2.children().show(); var selectedItem = $($dropdown1).val(); if (selectedItem != "") $('select[name="dropdown2"] option[value="' + selectedItem + '"]').hide(); }); $dropdown2.change(function() { $dropdown1.children().show(); var selectedItem = $($dropdown2).val(); if (selectedItem != "") $('select[name="dropdown1"] option[value="' + selectedItem + '"]').hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="dropdown1"> <option></option> <option value="1">Test 1</option> <option value="2">Test 2</option> <option value="3">Test 3</option> </select> <select name="dropdown2"> <option></option> <option value="1">Test 1</option> <option value="2">Test 2</option> <option value="3">Test 3</option> </select> 

Here's an approach that stores a set of the options on page load then filters the alternate select when a change is made. 这是一种在页面加载时存储一组选项,然后在进行更改时过滤替代选择的方法。 It works in both directions for changes made to either select 它在两个方向上都起作用,可以对任一选择进行更改

var $drops = $('.drop'),
// store a set of options
$options = $drops.eq(1).children().clone();

$drops.change(function(){
    var $other = $drops.not(this),
        otherVal = $other.val(),
        newVal = $(this).val(),
        $opts = $options.clone().filter(function(){
           return this.value !== newVal; 
        })
     $other.html($opts).val(otherVal);    
});

Values will also be maintained and this is 2 directional so a change in either will filter the other 值也将保持不变,并且这是2向的,因此任何一个更改都会过滤另一个

DEMO 演示

 var $dropdown1 = $("select[name='dropdown1']"); var $dropdown2 = $("select[name='dropdown2']"); $dropdown1.change(function() { var selectedItem = $(this).val(); var $options = $("select[name='dropdown1'] > option").clone(); $("select[name='dropdown2']").html($options); $("select[name='dropdown2'] > option[value="+selectedItem+"]").remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <select name="dropdown1"> <option></option> <option value="1">Test 1</option> <option value="2">Test 2</option> <option value="3">Test 3</option> </select> <select name="dropdown2"> <option></option> <option value="1">Test 1</option> <option value="2">Test 2</option> <option value="3">Test 3</option> </select> 

Demo 演示版

Well, this code will find option by value in necessary selects and remove it. 好的,此代码将在必要的选择中按值查找选项并将其删除。

http://codepen.io/anon/pen/LVqgbO http://codepen.io/anon/pen/LVqgbO

 var $dropdown1 = $("select[name='dropdown1']"); var $dropdown2 = $("select[name='dropdown2']"); var populateDropdown = function(element) { element.find("option").remove(); element.append("<option></option>"); // There should be real data for (var i = 1; i <= 3; i++) { element.append("<option value='" + i + "'>Test " + i + "</option>"); } } var getOptionProps = function(element) { var selectedValue = element.val(); var selectedText = element.find("option[value=" + selectedValue + "]").text(); return { text: selectedText, value: selectedValue }; } var removeOptionWithValue = function(element, value) { element.find("option[value='" + value + "']").remove(); } $dropdown1.on("change", function () { var selectedProps = getOptionProps($(this)); populateDropdown($dropdown2); removeOptionWithValue($dropdown2, selectedProps.value); }); $dropdown2.on("change", function () { var selectedProps = getOptionProps($(this)); populateDropdown($dropdown1); removeOptionWithValue($dropdown1, selectedProps.value); }); 

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

相关问题 在下拉列表 1 中选择时,我从下拉列表 2 中隐藏选项不起作用? - My Hide option from dropdown 2 when selected in dropdown 1 is not working? 当从下拉菜单中选择选项时,如何使用jQuery将所选选项隐藏到另一个下拉菜单中 - When selected option from dropdown then how to hide that selected option into another dropdown using jQuery 从下拉菜单中未选择任何选项时禁用按钮 - Disable button when no option is selected from dropdown HTML表单字段根据从下拉菜单中选择的选项进行显示/隐藏 - HTML Form field show/hide as per option selected from dropdown jQuery:根据从下拉列表中选择的选项显示/隐藏元素 - jQuery: Show/Hide Elements based on Selected Option from Dropdown 如何根据下拉列表中的选定选项隐藏单个 div - How to hide single div based on selected option from dropdown 当用户再次单击下拉菜单时,如何隐藏当前选择的li项目? - How to hide currently selected li item from being an option when user clicks dropdown again? 从动态表单 Angular 内的下拉列表中选择特定选项时显示隐藏文本框 - Show hide text box when specific option is selected from dropdown inside dynamic form Angular 在另一个下拉列表中选择选项时显示下拉列表 - Show dropdown when option is selected in the another dropdown 选择时下拉选项会更改 - dropdown option changes when selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM