gpt4 book ai didi

html5 数据列表仅选择预定义的选项

转载 作者:可可西里 更新时间:2023-11-01 14:45:58 29 4
gpt4 key购买 nike

我正在使用 HTML5 datalist允许通过自动完成和过滤功能从大列表中进行选择。但我只想允许从预定义选项中进行选择。参见 fiddle 演示 http://jsfiddle.net/tharas/rrkdu8pk/ .我希望用户仅从 <option> 中指定的值中进行选择标签。可能吗?

最佳答案

如果不在选项中,您可以在输入上使用“onchange”来重置值:

<body>
<!-- use 'required' for not letting the user submit the empty input
by hitting the enter button too quick-->
<input list="browsers" onchange="resetIfInvalid(this);" required >
<datalist id="browsers">
<option value="Edge">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</body>
<script>
function resetIfInvalid(el){
//just for beeing sure that nothing is done if no value selected
if (el.value == "")
return;
var options = el.list.options;
for (var i = 0; i< options.length; i++) {
if (el.value == options[i].value)
//option matches: work is done
return;
}
//no match was found: reset the value
el.value = "";
}
</script>

关于html5 数据列表仅选择预定义的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30499199/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com