<input id="test" type="text" list="list">
<datalist id="list">
<option value="A">First option</option>
<option value="B">Second option</option>
</datalist>
$("input").keyup(function(event) {
//any event like this for example...
console.log(event.target.value);
});
我不希望在用户单击选项后触发此事件。即使不太可能工作(javascript),也已经尝试了以下操作:
$("option").click(function(e) {
e.preventDefault();
e.stopPropagation();
});
另外,为什么事件会触发,因为我使用的是keyup
事件而不是input
?...
用Keypress
.
更新
您需要从事件内部而不是 onload 中调用值
$("input").keypress(function(event) {
//any event like this for example...
console.log("Hello from the other side");
});
$('#test').change(function(){ // you need to call the value from the event inside not a onload
console.log(document.getElementById("test").value) //no need
console.log($(this).val()) // jquery alredy present so simple use that
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="test" type="text" list="list">
<datalist id="list">
<option value="A">First option</option>
<option value="B">Second option</option>
</datalist>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句