javascript 即使重新选择相同的选项,也为选择运行更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7742739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run change event for select even when same option is reselected
提问by Spencer
Basically, I have drop down menu that looks like this:
基本上,我有如下所示的下拉菜单:
<select>
<option>0</option>
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
</select>
I am trying to write a function that is fired even when you select the same option, i.e. even if the drop-down is opened and re-select the selected option, I want it to execute the function.
我正在尝试编写一个即使在您选择相同选项时也会触发的函数,即即使打开下拉列表并重新选择所选选项,我也希望它执行该函数。
采纳答案by pimvdb
If you mean selecting with the mouse, you can use mouseup
. However, it will fire when the select box is being opened as well, so you'll need to keep track of the amount of times it was fired (even: select is being opened, odd: select is being closed): http://jsfiddle.net/T4yUm/2/.
如果您的意思是用鼠标选择,则可以使用mouseup
. 但是,它也会在打开选择框时触发,因此您需要跟踪触发的次数(偶数:选择正在打开,奇数:选择正在关闭):http:/ /jsfiddle.net/T4yUm/2/。
$("select").mouseup(function() {
var open = $(this).data("isopen");
if(open) {
alert(1);
}
$(this).data("isopen", !open);
});
回答by James Ellis-Jones
pimvdb's answer was a big help for me but I added a couple of extra bits to deal with keyboard activation and navigation away from the select:
pimvdb 的回答对我有很大帮助,但我添加了一些额外的位来处理键盘激活和远离选择的导航:
$('select').mouseup(... as in pimvdb... )
.blur(function() {
$(this).data('isopen', false);
}).keyup(function(ev) {
if (ev.keyCode == 13)
alert(1);
});
The blur handler deals with navigating away from the select while the dropdown is open, the keyup handler deals with when you change the select's value with the keyboard. The behaviour is then that the user is only considered to have finally selected the value when they click return to navigate away. If you want a different behaviour with the keyboard that shouldn't be hard to do.
当下拉菜单打开时,模糊处理程序处理从选择导航离开,当您使用键盘更改选择的值时,keyup 处理程序处理。然后行为是当用户单击返回导航离开时,才认为用户最终选择了该值。如果您想要键盘的不同行为,那应该不难做到。
回答by s4y
select
isn't meant to be used this way — there are hacks you can use to get this kind of behavior in most cases, like tracking mouse and keyboard events on the select
, but there's no guarantee they'll keep working, or work on every platform.
select
不打算以这种方式使用 - 在大多数情况下,您可以使用一些技巧来获得这种行为,例如在 上跟踪鼠标和键盘事件select
,但不能保证它们会继续工作,或在每个平台上工作.
I would suggest either…
我建议要么...
- Resetting the
select
to its default value on change, and using some other text to indicate which one is “selected”, or - using a control other than
select
.
select
在更改时将重置为其默认值,并使用其他一些文本来指示哪个被“选中”,或- 使用
select
.以外的控件。
Can you describe the end goal a little more detail?
你能更详细地描述一下最终目标吗?
回答by John Strickler
An alternative, use the .blur()
event -- http://jsfiddle.net/VKZb2/4/
另一种选择,使用.blur()
事件 ——http://jsfiddle.net/VKZb2/4/
Pro
亲
This will fire either way.
无论哪种方式,这都会触发。
Con
骗局
It only fires once the control loses focus.
只有在控件失去焦点时才会触发。
回答by Rico
I ran into the same issue. I just added a click event to the option tags and change the value of the select:
我遇到了同样的问题。我刚刚在选项标签中添加了一个点击事件并更改了选择的值:
$("#select option").mouseup(function() {
$(this).parent().val($(this).attr("value"));
// do something.
});
NOTE: This doesn't work on iPhone or iPad devices.
注意:这不适用于 iPhone 或 iPad 设备。