Html 输入列表选择更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15147728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input List Selection Changed Event
提问by swamprunner7
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.
我尝试这样做onchange="MySuperFunction();"
,但这仅在选择了一个项目然后列表未聚焦时才有效。
回答by Explosion Pills
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:
该input
事件应该适合您的需要。据我了解,您不能直接使用数据列表,但它通过list
属性连接到输入。此事件绑定将继续该输入:
document.getElementById('browsers-input').addEventListener('input', function () {
console.log('changed');
});
回答by Olivier Royo
To get the same effect of "Explosion Pills" solution using JQuery style:
要使用 JQuery 样式获得与“Explosion Pills”解决方案相同的效果:
$("#browsers-input").on("input", MySuperFunction);
回答by martriay
Usin jQuery
you could use .change
http://jquery.com/
使用设jQuery
你可以使用.change
http://jquery.com/
$('datalist#browsers').change(MySuperFunction);
or
或者
$('datalist#browsers').change(function(){
// stuff
});