最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do detect input field changed when selecting from a datalist? - Stack Overflow

programmeradmin4浏览0评论

I have an input with attached datalist:

<input type="text" list="mylist">

<datalist id="mylist">
<option>1
<option>2
</datalist>

When I choose an option from the list then the change event doesn't fire on the input element. So how can I detect input's change?

I have an input with attached datalist:

<input type="text" list="mylist">

<datalist id="mylist">
<option>1
<option>2
</datalist>

When I choose an option from the list then the change event doesn't fire on the input element. So how can I detect input's change?

Share Improve this question edited Feb 13, 2015 at 20:18 Yanick Rochon 53.6k29 gold badges145 silver badges225 bronze badges asked Feb 13, 2015 at 20:10 TomTom 3,0543 gold badges44 silver badges73 bronze badges 6
  • you can use Jquery to detect the change. eg $(‘#input-or-select-element-id‘).change(function(){}); – Ji_in_coding Commented Feb 13, 2015 at 20:12
  • it sounds like he's already using the $.change() function – CodeGodie Commented Feb 13, 2015 at 20:13
  • No value set - therefore no change is happening? – below9k Commented Feb 13, 2015 at 20:13
  • @below9k that sounds right.. he needs to show his code. – CodeGodie Commented Feb 13, 2015 at 20:14
  • @CodeGodie just FYI, the way of talking about jquery functions that belong to the matching set is $.fn.change – George Mauer Commented Feb 13, 2015 at 20:16
 |  Show 1 more ment

5 Answers 5

Reset to default 5

You can capture the input event:

document.querySelector('input').addEventListener('input', function() {
  alert('Changed!');
});
<input type="text" list="mylist">

<datalist id="mylist">
<option>1
<option>2
</datalist>

Update

I didn't notice the jQuery tag at first, and @YanickRochon made a good point that this should also capture the change event.

New Snippet

$('input').on('input change', function() {
  alert('Changed!');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" list="mylist">

<datalist id="mylist">
<option>1
<option>2
</datalist>

$(document).ready(function(){

  $('input').on('input', function () {
      alert("changed");
   })

});

Fiddle

You can use input event, like this

$('#input-list').on('input change', function () {
  console.log($(this).val());
})

Example

All other answers are not differentiating between input scenarios

The question is about how to specifically know when an input was changed by a datalist and not by anything else. This is very important to differentiate.

The below code will absolutely differentiate between a user input either by typing, pasting via the keyboard or via mouse or any other way, and between actually selecting an option from a <datalist>

var noneDatalistInput;

$('input').on('keydown paste', onNoneDatalistInput)
          .on('input', onInput);

// raise a flag
function onNoneDatalistInput(e){
    if( e.key == "Enter" ) return;
    if( noneDatalistInput ) clearTimeout(noneDatalistInput);
    noneDatalistInput = setTimeout(function(){ noneDatalistInput = null }, 50);
}

function onInput(){
    var isDatalistInput = !noneDatalistInput;
    console.log('was a datalist option selected? ', isDatalistInput);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" list="mylist">

<datalist id="mylist">
  <option>1</option>
  <option>2</option>
</datalist>

are you listening to INPUT change? or DataList Change?

// wont work
$("#myList").on('change', function () {
    alert("yay!");
});

// fires yay! when change value (after focus out)
$("#myText").on('change', function () {
    alert("yay!");
});

check out this fiddle

发布评论

评论列表(0)

  1. 暂无评论