[英]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();
}
});
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向的,因此任何一个更改都会过滤另一个
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>
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.