多次选择同一个选项/onchange 不改变 [英] Select the same option multiple times / onchange without changing
问题描述
我有一个选择,每次选择一个选项时我都需要触发一些 js,即使它已经被选择.示例:
I have a select, and I need to fire some js each time an option is selected, even if it is already selected. Example:
<select id="select_one">
<option value="">Choose One...</option
<option value="one">One</option>
<option value="two">Two</option>
</select>
如果有人选择一",我想触发 functionOne().但是,虽然选择了一",但如果用户再次选择一",我想再次触发 functionOne().所以,这意味着 onChange 将不起作用,因为选择没有改变.
If someone selects "one" I want to fire functionOne(). But, while "one" is selected, if the user selects "one" again I want to fire functionOne() again. So, that means onChange won't work, since the selection isn't changing.
有人有什么想法吗?
推荐答案
与当前的一些答案相反,您实际上并没有在
Contrary to some of the current answers, you don't actually get a click
event on <option>
in IE.
检测已选择的选项被点击的情况的唯一可靠方法是将 onclick
处理程序放在 上.当然,这也会检测对 select 元素的任何其他点击,因此根据
functionOne()
正在执行的操作,这也可能不安全.
The only reliable way to detect the case of an option being clicked that was already selected is to put the onclick
handler on the <select>
. Naturally this will also detect any other clicks on the select element, so depending on what functionOne()
is doing this may not be safe either.
使用看起来像选择但不是选择的东西可能会更好,例如带有按钮的弹出式 div.你想做什么?如果您尝试执行跳转菜单",选择一个选项会导航到新页面:不要,这是一种旧的、信誉不佳的机制,存在严重的可用性问题.
You might be better off with something that looks like a select but isn't, such as a pop-up div with buttons on it. What is it you are trying to do? If you're trying to do a "jump menu" where selecting an option navigates to a new page: don't, it's an old and discredited mechanism with serious usability problems.
这篇关于多次选择同一个选项/onchange 不改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!