简体   繁体   English

如何在单击数据列表选项列表时触发事件?

[英]How to fire an event on clicking data list option list?

I have a datalist that looks like :我有一个看起来像的数据列表:

 <datalist id="foodlist"> <option value="one" ></option <option value="two" ></option> <option value="three" ></option> </datalist> <input type="text" list="foodlist" autocomplete=true id="inputItem"/>

I want an event to fire when user selects on of the option in the list using JavaScript.我希望在用户使用 JavaScript 选择列表中的选项时触发一个事件。

How to achieve it?如何实现?

onClick, onChange does not seem to work. onClick,onChange 似乎不起作用。

I know this is kind of old, but I thought I should document it somewhere.我知道这有点旧,但我想我应该在某处记录它。 If you're trying to detect input from a dropdown selection rather than a click per se, you can use the "input" event and check the type of the passed event object - cut/paste/keypress input will pass an "InputEvent" object, whereas a datalist selection will pass a generic "Event" object.如果您试图从下拉选择中检测输入而不是点击本身,您可以使用“输入”事件并检查传递的事件对象的类型 - 剪切/粘贴/按键输入将传递一个“InputEvent”对象,而数据列表选择将传递一个通用的“事件”对象。

 var textbox = document.getElementById("inputItem"); textbox.addEventListener("input", function(e){ var isInputEvent = (Object.prototype.toString.call(e).indexOf("InputEvent") > -1); if(!isInputEvent) alert("Selected: " + e.target.value); }, false);
 <datalist id="foodlist"> <option value="one" ></option> <option value="two" ></option> <option value="three" ></option> </datalist> <input type="text" list="foodlist" autocomplete=true id="inputItem"/>

<datalist id="foodlist">    
    <option value="one" ></option>
    <option value="two" ></option>
    <option value="three"></option> 
</datalist>

<input id="txt" type="text" list="foodlist" autocomplete=true id="inputItem"/>

document.getElementById('txt').addEventListener('input', function () {
    console.log('changed'); 
    var val = document.getElementById("txt").value;
    alert(val);
});

The datalist tag is only supported in some browsers. datalist 标签仅在某些浏览器中受支持。 Here's simple trick to get selected value.这是获取选定值的简单技巧。

While this is 4 years old, I stumbled across it today, and ran into the cross browser issues that others have reported.虽然这是 4 岁,但我今天偶然发现了它,并遇到了其他人报告的跨浏览器问题。 I was able to solve this by listening to the keyup event on the input field, and then checking the event type.我能够通过监听输入字段上的 keyup 事件,然后检查事件类型来解决这个问题。

<input type="text" name="field_name" id="field_id" list="fields_list">
<datalist id="fields_list"></datalist>
<script>
var fieldInput = document.getElementById("field_id");

fieldInput.addEventListener('keyup', function(e) { if (isKeyboardEvent(e)) { myFunction(); } });

function isKeyboardEvent(e) {
    return (e instanceof KeyboardEvent);
}

function myFunction() {
   /* function that does something only on keyboard input */
}
</script>

Actual key strokes will be KeyboardEvent while a datalist option click will be simply be an Event .实际击键将是KeyboardEvent而数据列表选项单击将是Event

Confirmed working in Chrome, Firefox, and Edge.确认在 Chrome、Firefox 和 Edge 中工作。

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

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