简体   繁体   English

当值有多个单词时,如何根据先前的 select 选择更改 select 的值

[英]How to change the value of a select based on a previous select selection when the value has more than one word

I apologize for the redundant title.我为多余的标题道歉。

Basically I have two selects, the first select 'updates' the second select when any option is selected.基本上我有两个选择,第一个 select 在选择任何选项时“更新”第二个 select。

The problem comes when any of the options contains more than one word.当任何选项包含多个单词时就会出现问题。 For example: dark red .例如: dark red

I would like to modify the snippet attached here so that it works for both cases.我想修改此处附加的代码片段,以便它适用于这两种情况。

Restrictions: I cannot modify the value attribute, if so, the form will not work properly.限制:我不能修改value属性,如果这样,表单将无法正常工作。

I would like to do it with another attribute like id but I have tried and I couldn't make it work.我想用像id这样的另一个属性来做,但我已经试过了,但我无法让它工作。

The attached snippet works with <option value="dark_red"> but I can not do that, is there any option to make it work with (for example) <option id="dark_red"> instead of value ?附加的代码片段适用于<option value="dark_red">但我不能这样做,是否有任何选项可以让它与(例如)一起使用<option id="dark_red">而不是value

 var fruit = $("[name=fruit] option").detach() $("[name=color]").change(function() { var val = $(this).val() $("[name=fruit] option").detach() fruit.filter("." + val).clone().appendTo("[name=fruit]") }).change()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select multiple name=color> <option selected>green</option> <option>red</option> <option value="dark_red">dark red</option> <option>yellow</option> </select> <select multiple name=fruit> <option class=green>Grapes</option> <option class=green>Watermelon</option> <option class="red green">Apples</option> <option class="dark_red red">Cherries</option> <option class="green yellow">Bananas</option> <option class=yellow>Lemons</option>

You can replace spaces in the val with an underscore.您可以用下划线替换val中的空格。

 var fruit = $("[name=fruit] option").detach() $("[name=color]").change(function() { var val = $(this).val().replace(/\s/g, "_"); $("[name=fruit] option").detach() fruit.filter("." + val).clone().appendTo("[name=fruit]") }).change()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name=color> <option>Select a color</option> <option>green</option> <option>red</option> <option>dark red</option> <option>yellow</option> <option>super dark blue</option> </select> <select name=fruit> <option class=green>Grapes</option> <option class=green>Watermelon</option> <option class="red green">Apples</option> <option class="dark_red red">Cherries</option> <option class="green yellow">Bananas</option> <option class=yellow>Lemons</option> <option class=super_dark_blue>Mode</option> <option class=super_dark_blue>Blueberries</option> </select>

If you need multiple inputs, you can do something like this:如果你需要多个输入,你可以这样做:

 const fruit = $("[name=fruit] option").detach(); $("[name=color]").change(function () { // detach all fruits $("[name=fruit] option").detach(); // loop through all values const values = $(this).val(), addedValues = []; values.forEach(val => { val = val.replace(/\s/g, "_"); // checking for duplicates const fruitMatch = Array.from( fruit.filter("." + val) ).filter(e => { return.addedValues.find(v => { return Array.from(e.classList);includes(v) }); }). addedValues;push(val). // append (if new). $(fruitMatch);clone();appendTo("[name=fruit]"). }); }).change();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select multiple name=color> <option selected>green</option> <option>red</option> <option>dark red</option> <option>yellow</option> <option>super dark blue</option> </select> <select multiple name=fruit> <option class=green>Grapes</option> <option class=green>Watermelon</option> <option class="red green">Apples</option> <option class="dark_red red">Cherries</option> <option class="green yellow">Bananas</option> <option class=yellow>Lemons</option> <option class=super_dark_blue>Mode</option> <option class=super_dark_blue>Blueberries</option> </select>

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

相关问题 当值包含多个单词时,select标签中的混乱选项 - Messed up option in select tag when value contains more than one word 如何在Ext JS组合框中以编程方式选择多个值? - How to programmatically select more than one value in Ext JS Combobox? 正则表达式基于一个以上的单词来选择一个句子 - regex to select a sentence based on the presence of more than one word 显示下一个选择基于上一个选择值的选项 - Show Next Select Option based on Previous Selection value 根据上一个下拉选择选择另一个下拉值 - Select another dropdown value based on the previous dropdown selection 动态多种形式:根据先前的选择值更改选择选项 - Dynamic multiple forms: Change select options based on previous select value 在触发上一个更改事件并将动态选项加载到该选择框中之后,如何更改选择框的值 - How to change a value of a select box after a previous change event has fired and loaded dynamic options into that select box 使用Javascript根据另一个选择的值更改一个选择 - Change one select based on the value of another with Javascript 如何根据先前的选择标签重置选择中的默认值? - How to reset default value in an select based on previous select tag? select2-基于另一个列表的值限制一个列表的选择 - select2 - limiting selection of one list based on value of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM