当我在选择上使用向上或向下箭头时,为什么 jquery 更改事件不触发?
我正在使用 jquery 和 livequery 插件监听选择下拉列表的更改事件。
$(".myDropdown").livequery("change", function () {
});
我注意到的一件事(我正在使用 Firefox)是,
当我选择焦点时,事件处理程序不会因点击向上和向下箭头而触发(向上和向下箭头确实会更改条目)
为什么更改事件不发生当我上下移动箭头键时触发,是否有建议或解决方法?
I am listening to the change event of a select dropdown using jquery and the livequery plugin.
$(".myDropdown").livequery("change", function () {
});
one thing i noticed (i am using firefox) is that
The event handler doesn't fire from hitting the up and down arrows when i have the select focused (the up and down arrow DO change the entries)
Why doesn't the change event fire when i move the arrows key up and down and is there a recommendation or workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想我会总结一下。
如果您单击选择框中的某个项目,则您正在更改它。如果您通过键入向下浏览选择框,则只有按 Enter 键、单击关闭或按 Tab 离开后才会选择该选择框。我认为这在不同的浏览器中略有不同,但本质上,选择发生在“我已完全完成此表单字段并立即离开”事件之类的事件上。
如果您希望结果更直接,您可以像这样触发更改事件:
如果您希望在所有表单字段上发生这种情况,请尝试此选择器:
$(":input select textarea")
I suppose I'll summarize.
If you click an item in the select box you are changing it. If you go downward through the select box by keying through it is not selected until you press enter, click off or tab away. I suppose this differs slightly from browser to browser, but essentially the selection occurs on something like a "i'm completely done with this form field and moved away now" event.
If you would like the result to be more immediate you could either trigger the change event like this:
If you would like that to occur on all form fields try this selector:
$(":input select textarea")
以下代码(无论如何在 Firefox 3.6 上)同意 OP 的观点,但不同意评论者的观点:
每当您按下箭头键时,更改的值都会打印到日志中。
This following code (on Firefox 3.6 anyway) agrees with the OP and disagrees with the commenters:
Whenever you hit an arrow key, a changed value is printed to the log.
即使您正在更改值(正如 Malvolio 所证明的那样),在元素失去焦点之前更改事件不会被触发。当按下任意键时,以下代码片段将手动触发更改事件:
Change events are not on fired until the element loses focus, even though you are changing the value (as Malvolio proved). The following code snippet will manually fire the change event when any key is pressed:
如果马伏里奥说的是真的,那么应该可以
http://jsfiddle.net/tW6Su/2/ 作为证明
If what Malvolio says is true, then that should work
http://jsfiddle.net/tW6Su/2/ as proof