[英]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.