简体   繁体   English

datalist 如何判断下拉菜单中是否选择了输入

[英]datalist how to tell if selected input in dropdown

 <label>Choose a browser from this list:
    <input id = "a" list="browsers" name="myBrowser" 
      style="width: 400px;" onchange="castvote()"  /></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>

function castvote() {
    var datalist = document.getElementById('a');
   // var datalist = document.getElementById("a").children; // this did not work as well
    console.log("Chrome" in datalist);
    }

when i select Chrome or any other options and check to see if it is one of the options i wanted (one of the drop down options) the console is returning false.当我 select Chrome 或任何其他选项并检查它是否是我想要的选项之一(下拉选项之一)时,控制台返回 false。 How do I make it return true (detect that one of the options I selected is indeed the dropdown options)?如何让它返回 true(检测我选择的选项之一确实是下拉选项)? If possible no Jquery please如果可能,请不要 Jquery

 function castvote() { var datalist = document.getElementById('a'); // var datalist = document.getElementById("a").children; // this did not work as well // console.log("Chrome" in datalist); var browserChildren = document.getElementById('browsers').children var flag = false for(let i = 0; i < browserChildren.length; i++){ flag = browserChildren[i].value === datalist.value || flag } console.log(flag) }
 <label>Choose a browser from this list: <input id = "a" list="browsers" name="myBrowser" style="width: 400px;" onchange="castvote()" /></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>

since you gave the options in the, you can't get something different to the options, and taking this in account, in the logic you described all the time the result will be true, if you want to get the selected value you can use the event object from the event listener.由于您在 中提供了选项,因此您无法获得与选项不同的东西,并且考虑到这一点,在您一直描述的逻辑中,结果将是真实的,如果您想获得您可以使用的选定值来自事件侦听器的事件 object。 Here is an example of that.这是一个例子。

As an extra note, is better if you add the listeners with "addEventListener" because you will get more control of the events, and add more than one event per element.作为额外说明,如果您使用“addEventListener”添加侦听器会更好,因为您将获得对事件的更多控制,并为每个元素添加多个事件。

 const browsersDataList = document.getElementById("browser-choice"); browsersDataList.addEventListener("change", castvote); function castvote(event) { console.log(event.target.value) }
 #browser-choice { width: 400px; }
 <label for="browser-choice">Choose a browser from this list:</label> <input list="browsers" id="browser-choice" name="browser-choice" /> <datalist id="browsers"> <option value="Chrome"> <option value="Firefox"> <option value="Internet Explorer"> <option value="Opera"> <option value="Safari"> <option value="Microsoft Edge"> </datalist>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM