如何获取数据列表的更改事件? [英] How do I get the change event for a datalist?

查看:82
本文介绍了如何获取数据列表的更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据列表,需要检测用户何时从下拉列表中选择内容. 有人问了类似的问题但我需要它,以便仅当用户从数据列表中选择某项时,才会触发该事件.如果他们在输入中键入内容,那么我不希望触发该事件. (请注意,在我链接的问题的可接受答案中,它们绑定了输入,这不是我想要的).我已经尝试过(没有成功):

I am using a datalist and need to detect when the user selects something from the drop-down list. A similar question has been asked BUT I need it so that the event fires ONLY when the user selects something from the datalist. If they type something in the input then I do NOT want the event to fire. (Notice in the accepted answer to the question I linked that they bind the input, which is not what I want). I've tried (with no success):

<datalist>
    <option>Option 1 Here</option> 
    <option>Option 2 Here</option>
</datalist>


$(document).on('change', 'datalist', function(){
   alert('hi');
});

这个问题与建议的问题不同,因为它是完全不同的问题.

This question is different than the suggested question because it's a completely different question.

推荐答案

您可以在更改时手动进行检查.但是您需要检查数据列表输入的更改.

You can manually check it on change. But you need to check change of the input of datalist.

$(document).on('change', 'input', function(){
    var options = $('datalist')[0].options;
    var val = $(this).val();
    for (var i=0;i<options.length;i++){
       if (options[i].value === val) {
          alert(val);
          break;
       }
    }
});

FIDDLE

这篇关于如何获取数据列表的更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆