<select> 上的 jQuery change()和火狐 [英] jQuery change() on &lt;select&gt; and firefox

查看:23
本文介绍了<select> 上的 jQuery change()和火狐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单,当它改变时会触发 ajax 调用:

I have a dropdown that triggers an ajax call when its changed:

$('.travel-to').change(function(){  
    $.ajax({
        type: "GET",
        url: "/inc/rates/rates-viewer.php",
        data: "shtech=y&c_name="+escape($(this).val()),
        success: function(html){
            $(".rates-viewer").html(html);
            $(".rates-viewer tr.numbers td").css({ opacity: 0 }).fadeTo("slow",1);
        }
    });
});

我的问题是,在 Firefox 中,使用向上/向下光标键循环下拉选项,不会触发 js onChange() 事件.在 IE 中没问题.

My problem is, in Firefox, using the up/down cursor keys to cycle through the dropdown options, doesn't trigger the js onChange() event. It's ok in IE.

如何让 Firefox 将向上/向下光标视为 onChange?我可以在事件上执行一个/或一个,以使 onChange 或按键触发相同的事情吗?

How can I make Firefox see the up/down cursors as an onChange? Can I do an either/or on the event, to make either an onChange or a keypress trigger the same thing?

谢谢.

推荐答案

您实际上是在利用 IE 中的错误.Firefox 正确支持 onChange,因为它不应该在浏览器失去选择字段的焦点之前触发.(我回答了一个昨天关于这个问题的问题,事实上.)将 onChange 与选择一起使用实际上有点危险,特别是因为只有键盘的用户无法跳过某些选项.(最重要的是,鼠标滚轮似乎会根据多个答案旋转,但实际上它会为它在 IE 上经过的每个条目触发 onChange.)

You're actually taking advantage of a bug in IE. Firefox supports onChange correctly, as it's not supposed to fire until the browser loses focus of the select field. (I answered a question yesterday about this very issue, in fact.) It's actually kind of dangerous to use onChange with a select, especially because there's no way for keyboard only users to skip some options. (On top of that, the mouse wheel seems to spin by multiple answers, but it actually fires onChange for each entry it passes by on IE.)

如果您真的希望在有人向上或向下按下时触发该事件,我会挂钩 onKeyPress 或 onKeyDown 事件以在按下向上"或向下"键时触发.

If you really want the event to fire whenever someone presses up or down, I would hook into the onKeyPress or onKeyDown events to fire whenever the "up" or "down" key is pressed.

这篇关于<select> 上的 jQuery change()和火狐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆