【发布时间】:2019-03-03 07:12:26
【问题描述】:
我的项目中有一个输入字段和一个选择字段。我想根据用户输入的内容(字母和数字的组合)动态更改选择选项。用户也可以自己选择,动态变化是一种帮手。
<input id="userInput" type="text" class="form-control" placeholder="Type here">
<select id="sel" class="form-control form-control-xl">
<option selected>Choose one</option>
<option value="1">Dog</option>
<option value="2">Cat</option>
<option value="3">Lizard</option>
<option value="4">Fish</option>
</select>
该选项基于这样的逻辑
var userInput = document.getElementById('userInput').value;
var sel = document.getElementById('sel');
if (userInput firstletter is Z and followed by numbers) {
$('#sel').val('1').change(); // Dog
} else if (userInput beginsWith === AB and endsWith === YZ with numbers between) {
$('#sel').val('2').change(); // Cat
} else if (userInput endsWith === XX) {
$('#sel').val('3').change(); // Lizard
} else if (userInput beginsWith === 222) {
$('#sel').val('4').change(); // Fish
}
将选项更改为类似于带有自动建议的搜索字段的正确方法是什么?
【问题讨论】:
-
使用正则表达式.......
-
输入的字母数字字符串与
<select>中的选项有什么关系? -
字母数字字符串的类型由订单字符串决定选项,例如。
Z12345id 代表狗,AB12345YZ代表猫。
标签: javascript dynamic drop-down-menu