[英]datalist how to tell if input is one of the options
I was wondering why why I select 2 with a select tag it returns true in the console.我想知道为什么我 select 2 带有 select 标签它在控制台中返回 true。 But Whenever I select 2 in the datalist it returns false in the console.
但是每当我在数据列表中的 select 2 时,它都会在控制台中返回 false 。
In short I need the datalist to return tru.简而言之,我需要数据列表来返回 tru。 I need to it be true so that when the user enters in the input I can ensure that the user has enter in a option I wanted (one of the drop down menu options )
我需要它是真的,这样当用户输入输入时,我可以确保用户输入了我想要的选项(下拉菜单选项之一)
function castvote() { var selected = document.getElementById('vote'); console.log("Chrome" in selected); }
<input list="vote" onchange="castvote()" id="voteInput"> <datalist id="vote" > <option value="Chrome"> </datalist> <label>Choose a browser from this list: <input id = "a" list="browsers" name="myBrowser" style="width: 400px;" onclick="test()" /></label> <datalist id="browsers"> <option value="Chrome"> <option value="Firefox"> <option value="Internet Explorer"> <option value="Opera"> <option value="Safari"> <option value="Microsoft Edge"> </datalist>
You should try this.你应该试试这个。 I just put (
.children
) at the getElementById()
to get all children of datalist and the onChange()
function put at the input tag.我只是将(
.children
)放在getElementById()
以获取 datalist 的所有子项,并将onChange()
function 放在输入标签上。
<input list="vote" onchange="castvote()" id="voteInput">
<datalist id="vote" >
<option value="2">
<option value="3">
<option value="4">
<option value="5">
<option value="6">
</datalist>
<script>
function castvote() {
var datalist = document.getElementById('vote').children;
console.log("2" in datalist);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.