且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

输入列表选择更改事件

更新时间:2023-12-04 14:14:58

/ code>事件应该为您需要的工作。据了解,您不能直接使用datalist,而是通过列表属性连接到输入。此事件绑定将继续输入:

  document.getElementById('browsers-input')。addEventListener('input' function(){
console.log('changed');
});

http:// jsfiddle.net/vccfv/


I have this example:

<datalist id="browsers">
  <option value="Internet Explorer">
  <option value="Firefox">
  <option value="Google Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>

I need to catch an event, when the user selects option (with mouse or keyboard).

I tried to do onchange="MySuperFunction();" but this works only when an item is selected and then the list is unfocused.

The input event should work for what you need. As I understand, you can't use a datalist directly, but it is connected to an input by the list attribute. This event binding would go on that input:

document.getElementById('browsers-input').addEventListener('input', function () {
   console.log('changed'); 
});

http://jsfiddle.net/vccfv/