简体   繁体   English

检测用户何时从下拉菜单中选择一个选项

[英]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: 我努力了:

  • Listening for clicks on the <option> tags, but it won't work for users using a keyboard or a touch device 监听<option>标记的点击,但不适用于使用键盘或触摸设备的用户
  • on an interval, looping though the <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.

相关问题 当用户选择其他选项时,HTML选择的选项不会更改 - HTML selected option not changing when user chooses different option 在下拉菜单1中选中后,从下拉菜单2中隐藏选项 - Hide option from dropdown 2 when selected in dropdown 1 如何根据用户选择的选项使用JQuery提交表单 - How to submit a form using JQuery depending on which option a user chooses 当用户从下拉菜单中选择一个选项时,如何更改按钮上的文本? (angular.js) - How can I change the text on the button when user selects one option from the dropdown? (angular.js) 当用户再次单击下拉菜单时,如何隐藏当前选择的li项目? - How to hide currently selected li item from being an option when user clicks dropdown again? 使用jQuery:当用户从下拉列表中进行选择时,如何获取选项值 - Using jQuery: How to grab option values when user selects from the dropdown 当用户从 React 的下拉列表中选择一个选项时如何启用另一个输入字段 - How to enable another input field when a user selects an option from dropdown in React 当用户从下拉菜单中选择一个选项时,如何更新任何归档的文本? - How to update any text filed when the user is select an option from a dropdown-menu? 从下拉菜单中未选择任何选项时禁用按钮 - Disable button when no option is selected from dropdown 当我从下拉菜单中选择 select 选项时,隐藏的下拉菜单不可见 - Hidden Dropdown is not visible when i select option from the dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM