[英]Detecting when the user chooses an option from a dropdown
I have a classic HTML select box: 我有一个经典的HTML选择框:
Show:
<select name="show" id="showThreads">
<option value="all" selected="selected">All</option>
<option value="new">Only unread</option>
</select>
Now, I need JavaScript to make an Ajax request when the user changes the selection in the box (without jQuery). 现在,当用户更改框中的选择(没有jQuery)时,我需要JavaScript发出Ajax请求。 I have tried: 我努力了:
<option>
tags, but it won't work for users using a keyboard or a touch device 监听<option>
标记的点击,但不适用于使用键盘或触摸设备的用户 <option>
tags and checking if the selected one changes, but it seemed to trigger even when I simply hovered over the second option. 每隔一段时间,循环遍历<option>
标记并检查所选的<option>
是否更改,但是即使我只是将鼠标悬停在第二个选项上,它似乎也会触发。 Is there a way of doing this that will work on all browsers/devices? 有没有一种方法可以在所有浏览器/设备上使用? Thanks! 谢谢!
在<select>
标记上侦听onChange
。
Try the "change"
event. 尝试"change"
事件。
document.getElementById("showThreads").onchange = function() {
};
function yourAjaxCall(something) {
}
<select name="choice1" size="1" onchange="yourAjaxCall(this);">
<option value="one">first</option>
<option value="two">second</option>
<option value="three">third</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.