简体   繁体   English

如果下拉选项不可用,如何隐藏它?

[英]How to hide dropdown option if it is not available?

I have three dropdowns.我有三个下拉菜单。 Suppose there is the combination of "M / Grey" but not "M / Red" in the name="id" dropdown.假设在name="id"下拉列表中有“M / Grey”但没有“M / Red”的组合。 If I select "M" in the first .variant dropdown the "Red" option should not be shown in the second .variant dropdown.如果我在第一次选择“M” .variant下拉列表中的“红”选项不应该在第二显示.variant下拉。

 $(document).ready(function () { $('.variant').change(function(){ var options = $(this).find('.variant').children(":selected").get().map(function(el) { return el.value }).join(" / "); $('select[name="id"] option:contains(' + options + ')').prop('selected', true); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <form action="#" method="post"> <select class="variant"> <option value="S">S</option> <option value="M">M</option> <option value="L">L</option> </select> <select class="variant"> <option value="Grey">Grey</option> <option value="Red">Red</option> <option value="White">White</option> </select> <br> <select name="id"> <option value="M / Grey">M / Grey</option> <option value="L / Grey">L / Grey</option> <option value="S / Red">S / Red</option> <option value="L / Red">L / Red</option> <option value="M / White">M / White</option> <option value="L / White">L / White</option> </select> </form>

Had the same idea as @biberman but my code allows you to change your .variants 's second class to whatever you like - it could be <select class="variant size"> or <select class="variant variant1"> or whatever与@biberman 有相同的想法,但我的代码允许您将.variants的第二个类更改为您喜欢的任何内容 - 它可以是<select class="variant size"><select class="variant variant1">或其他任何内容

 $(document).ready(function () { function gatherCombinations () { var combos = $('select[name="id"] > option'); var result = []; for (var i = 0; i < combos.length; i++) { var comboVal = $(combos[i]).val(); var comboValArr = comboVal.split(" / "); result.push(comboValArr); } return result; } function isExistingCombination (var1, var2) { var combinations = gatherCombinations(); for (var i = 0; i < combinations.length; i++) { var combo = combinations[i]; if (combo.indexOf(var1) >= 0 && combo.indexOf(var2) >= 0) { return true; } } return false; } $('.variant').on('change', function() { var variantType = $(this).attr('data-var'); var variantVal = $(this).val(); var otherVariants = $('.variant:not(.' + variantType + ')'); var otherVariantsOptions = $('.variant:not(.' + variantType + ') > option'); otherVariantsOptions.show(); for (var i = 0; i < otherVariantsOptions.length; i++) { var otherVariantOptionVal = $(otherVariantsOptions[i]).val(); if (isExistingCombination(variantVal, otherVariantOptionVal) == false) { $(otherVariantsOptions[i]).hide(); } } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="#" method="post"> <select class="variant size" data-var="size"> <option value="S">S</option> <option value="M">M</option> <option value="L">L</option> </select> <select class="variant color" data-var="color"> <option value="Grey">Grey</option> <option value="Red">Red</option> <option value="White">White</option> </select> <br> <select name="id"> <option value="M / Grey">M / Grey</option> <option value="L / Grey">L / Grey</option> <option value="S / Red">S / Red</option> <option value="L / Red">L / Red</option> <option value="M / White">M / White</option> <option value="L / White">L / White</option> </select> </form>

First: You should add to each select a placeholder like an empty option .第一:您应该为每个选择添加一个占位符,例如空option Otherwise you couldn't trigger the change event if you select a preselected value (here S and Grey ).否则,如果您选择了一个预选值(此处为SGrey ),则无法触发change事件。


To update the other select.variant you have to compare its values (combined with the chosen value) with the values of the select[name="id"] .要更新另一个select.variant您必须将其值(与所选值组合)与select[name="id"]

You could achieve this with multiple solutions, for example data-attributes, the value strings or classes.您可以使用多种解决方案来实现这一点,例如数据属性、值字符串或类。 I used classes for the following solution.我将类用于以下解决方案。

<option class="m grey" value="M / Grey">M / Grey</option>

So you can simply check if there is an option that has the questionable class.因此,您可以简单地检查是否存在具有可疑类的选项。

if (!$('option').is('.' + val + '.' + color_val)) {...}

At the beginning of the change handler you have to "reset" the options of the other select.variant (remove all disabled attributes).change处理程序的开头,您必须“重置”其他select.variant的选项(删除所有禁用的属性)。

Working example:工作示例:

 $(document).ready(function() { $('.variant').change(function() { const val = this.value.toLowerCase(); const variant_type = this.className.replace('variant', '').trim(); $('.variant:not(.' + variant_type + ') option').attr('disabled', false); $('.variant:not(.' + variant_type + ') option').each(function() { const other_val = this.value.toLowerCase(); if (!$('option').is('.' + other_val + '.' + val)) { this.disabled = true; } }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <form action="#" method="post"> <select class="variant size"> <option></option> <option value="S">S</option> <option value="M">M</option> <option value="L">L</option> <option value="XL">XL</option> </select> <select class="variant color"> <option></option> <option value="Grey">Grey</option> <option value="Red">Red</option> <option value="White">White</option> </select> <br> <select name="id"> <option></option> <option class="m grey" value="M / Grey">M / Grey</option> <option class="l grey" value="L / Grey">L / Grey</option> <option class="s red" value="S / Red">S / Red</option> <option class="l red" value="L / Red">L / Red</option> <option class="m white" value="M / White">M / White</option> <option class="l white" value="L / White">L / White</option> <option class="xl red" value="XL / Red">XL / Red</option> </select> </form>


If you want to use data-attributes you have to modify the function a slightly bit.如果你想使用数据属性,你必须稍微修改一下函数。 The select.variant get instead of an additional class a data-type attribute. select.variant获取data-type属性而不是附加类。 For example:例如:

<select class="variant" data-type="size">

Combined example: ( data-attributes and classes )组合示例:(数据属性和类

 $(document).ready(function() { $('.variant').change(function() { const val = this.value.toLowerCase(); const variant_type = this.dataset.type; $('.variant:not([data-type="' + variant_type + '"]) option').attr('disabled', false); $('.variant:not([data-type="' + variant_type + '"]) option').each(function() { const other_val = this.value.toLowerCase(); if (!$('option').is('.' + other_val + '.' + val)) { this.disabled = true; } }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <form action="#" method="post"> <select class="variant" data-type="size"> <option value="S">S</option> <option value="M">M</option> <option value="L">L</option> <option value="XL">XL</option> </select> <select class="variant" data-type="color"> <option value="Grey">Grey</option> <option value="Red">Red</option> <option value="White">White</option> </select> <br> <select name="id"> <option class="m grey" value="M / Grey">M / Grey</option> <option class="l grey" value="L / Grey">L / Grey</option> <option class="s red" value="S / Red">S / Red</option> <option class="l red" value="L / Red">L / Red</option> <option class="m white" value="M / White">M / White</option> <option class="l white" value="L / White">L / White</option> <option class="xl red" value="XL / Red">XL / Red</option> </select> </form>


For a solution with data-attributes only the select[name="id"] gets instead of two classes two data-attributes, one for each possible data-type -value - in this example: data-size and data-color .对于具有data-attributes的解决方案,只有select[name="id"]获得两个数据属性而不是两个类,每个可能的data-type -value 一个 - 在此示例中: data-sizedata-color For example:例如:

<option data-size="S" data-color="Red" value="S / Red">S / Red</option>

To get the data-values you can use the dataset keyword.要获取数据值,您可以使用dataset关键字。 To select an element you can use attribute-selectors.要选择一个元素,您可以使用属性选择器。 For example:例如:

const variant_type = this.dataset.type;

$('.variant:not([data-type="' + variant_type + '"])')

With the data-type from the select.variant you could define which data-attribute of select[name="id"] you want to select.使用select.variantdata-type ,您可以定义要select[name="id"]数据属性。 For example:例如:

$('[data-' + other_type + '="' + other_val + '"]')

Working example:工作示例:

 $(document).ready(function() { $('.variant').change(function() { const val = this.value; const variant_type = this.dataset.type; const other_type = $('.variant:not([data-type="' + variant_type + '"])')[0].dataset.type; $('.variant:not([data-type="' + variant_type + '"]) option').attr('disabled', false); $('.variant:not([data-type="' + variant_type + '"]) option').each(function() { const other_val = this.value; if (!$('option').is('[data-' + variant_type + '="' + val + '"][data-' + other_type + '="' + other_val + '"]')) { this.disabled = true; } }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <form action="#" method="post"> <select class="variant" data-type="size"> <option></option> <option value="S">S</option> <option value="M">M</option> <option value="L">L</option> <option value="XL">XL</option> </select> <select class="variant" data-type="color"> <option></option> <option value="Grey">Grey</option> <option value="Red">Red</option> <option value="White">White</option> </select> <br> <select name="id"> <option></option> <option data-size="M" data-color="Grey" value="M / Grey">M / Grey</option> <option data-size="L" data-color="Grey" value="L / Grey">L / Grey</option> <option data-size="S" data-color="Red" value="S / Red">S / Red</option> <option data-size="L" data-color="Red" value="L / Red">L / Red</option> <option data-size="M" data-color="White" value="M / White">M / White</option> <option data-size="L" data-color="White" value="L / White">L / White</option> <option data-size="XL" data-color="Red" value="XL / Red">XL / Red</option> </select> </form>


If you want to auto update the third select when both select.variant are chosen you could define a data object.如果您想在选择两个select.variant时自动更新第三个选择,您可以定义一个数据对象。 For example:例如:

let selected = {};

With each call of the change handler you would update the object with the data-type and the value (add a new entry to the object or overwrite an existing one):每次调用change处理程序时,您都将使用data-type和值更新对象(向对象添加新条目或覆盖现有条目):

selected[variant_type] = val;

After that you would update the third select (add the selected attribute to the fitting option), but only if both select.variant were chosen, which means that both got an entry in the data object:之后,您将更新第三个select (将selected属性添加到拟合选项中),但select.variant是两个select.variant都被选中,这意味着两者都在数据对象中获得了一个条目:

if (selected[variant_type] && selected[other_type]) {...}

Working example:工作示例:

 $(document).ready(function() { let selected = {}; $('.variant').change(function() { const val = this.value; const variant_type = this.dataset.type; const other_type = $('.variant:not([data-type="' + variant_type + '"])')[0].dataset.type; selected[variant_type] = val; $('.variant:not([data-type="' + variant_type + '"]) option').attr('disabled', false); $('.variant:not([data-type="' + variant_type + '"]) option').each(function() { const other_val = this.value; if (!$('option').is('[data-' + variant_type + '="' + val + '"][data-' + other_type + '="' + other_val + '"]')) { this.disabled = true; } }); if (selected[variant_type] && selected[other_type]) { $('option[data-' + variant_type + '="' + selected[variant_type] + '"][data-' + other_type + '="' + selected[other_type] + '"]' )[0].selected = true; } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <form action="#" method="post"> <select class="variant" data-type="size"> <option></option> <option value="S">S</option> <option value="M">M</option> <option value="L">L</option> <option value="XL">XL</option> </select> <select class="variant" data-type="color"> <option></option> <option value="Grey">Grey</option> <option value="Red">Red</option> <option value="White">White</option> </select> <br> <select name="id"> <option></option> <option data-size="M" data-color="Grey" value="M / Grey">M / Grey</option> <option data-size="L" data-color="Grey" value="L / Grey">L / Grey</option> <option data-size="S" data-color="Red" value="S / Red">S / Red</option> <option data-size="L" data-color="Red" value="L / Red">L / Red</option> <option data-size="M" data-color="White" value="M / White">M / White</option> <option data-size="L" data-color="White" value="L / White">L / White</option> <option data-size="XL" data-color="Red" value="XL / Red">XL / Red</option> </select> </form>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM