简体   繁体   English

html5数据列表选项上的事件单击

[英]Event on html5 datalist option click

I need a datalist with my html5 input, and I want to create another event when the user click on one option of the datalist. 我需要一个带有html5输入的数据列表,并且我想在用户单击数据列表的一个选项时创建另一个事件。 I tried but it doesn't work :( 我试过了,但是不起作用:(

Here is a part of my code : 这是我的代码的一部分:

    <input name="adresse" type="text" id="formadresse" list="listadresses">
<datalist id="listadresses">
<option class="adresseoption" data-placeid="ChIJjwkhBaY_5kcRlWs1lhSC4tw" value="Louvres, France"></option>
<option class="adresseoption" data-placeid="ChIJD3uTd9hx5kcR1IQvGfr8dbk" value="Louvre Museum, Rue de Rivoli, Paris, France"></option>
<option class="adresseoption" data-placeid="ChIJ5V8P_KM_5kcRDQeUoTCtQ7U" value="Louvres, France"></option>
</datalist>

Whatever the jquery code i try, i fail into create the simplest result as an alert(). 无论我尝试使用哪种jQuery代码,我都无法创建最简单的结果作为alert()。

I have no exception thrown, the events I try to activate aren't recognized. 我没有抛出异常,我尝试激活的事件未被识别。

Thank you for your help ! 谢谢您的帮助 !

You can catch an event by watching the input event on your input with jQuery. 您可以通过使用jQuery监视输入中的输入事件来捕获事件。 This event will be trigger when the user change what is in the input and click on an item in your datalist. 当用户更改输入内容并单击数据列表中的项目时,将触发此事件。

$(document).ready(function(){
    $("#formadresse").on('input',function() {
        alert($(this).val());
    });
})

Working fiddle 工作提琴

Unfortunately as said here , there is no many possibilities with datalist... 不幸的是,正如这里所说的,datalist没有很多可能性...

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

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