如何查找导致 IE 中选择框 onChange 事件的原因 - 键盘或鼠标?
有什么方法可以找出导致 Internet Explorer (>= IE8) 中选择框上的 onChange 事件的原因 - 键盘或鼠标?
我有一个代码,当用户选择一个值时,它会执行一些操作,并且该代码在 Firefox 和 Chrome 中运行良好,但在 IE 中则不然(毫不奇怪,呵呵)。在 IE 中,只有当用户使用鼠标而不是键盘时它才能正常工作,因为这样它会在每次按键时触发 onchange 事件(而不是像普通浏览器那样在 Enter 上触发)。
因此,为了修复此行为,我需要知道事件是否是使用键盘触发的,然后我将对其进行过滤。
更新:
好的,玩了一下后我找到了一个很好的解决方案。将其发布在这里以防有人会发现它有用。下面的解决方案使用 jQuery,但也可以用纯 Javascript 完成。
这是导致问题的代码:
$("#mySelectBox").change(function () {
// Do something
});
这是我的解决方案。它可能并不完美,但它适用于我的情况。当然,事件处理程序可以链接在 jQuery 中。下面的代码存储选择的初始值,并使用它来避免在初始鼠标单击时执行某些操作 - 当用户展开选择框时。它还过滤除 Enter 之外的所有按键。
function doSomething(el) {
if (el.data["valueOnFocused"] !== el.val()) {
// Do something
}
}
$("#mySelectBox").focusin(function () {
$(this).data["valueOnFocused"] = $(this).val();
});
$("#mySelectBox").keyup(function (e) {
if (e.which === 13)
{
doSomething($(this));
}
});
$("#mySelectBox").click(function () {
doSomething($(this));
});
Is there some way to find out what caused the onChange event on select box in Internet Explorer (>= IE8) - keyboard or mouse?
I have a code which doing something when user selecting a value, and this code works great in Firefox and Chrome but not in IE (no surprise, huh). In IE it works fine only if user uses mouse but not a keyboard, because then it fires a onchange event on every keypress (not on Enter as normal browsers).
So, to fix this behavior I need to know if event is fired using a keyboard and then I will filter it.
Update:
Ok, after playing a bit I found a good solution. Posting it here in case someone will find it useful. Solution below using jQuery but it can be done in pure Javascript too.
This is a code which caused a problem:
$("#mySelectBox").change(function () {
// Do something
});
And this is my solution. It's probably not perfect, but it works in my case. And event handlers could be chained in jQuery, of course. The code below stores initial value of the select and uses it to avoid doing something on initial mouse click - when user expands a select box. Also it filters all keypresses except Enter.
function doSomething(el) {
if (el.data["valueOnFocused"] !== el.val()) {
// Do something
}
}
$("#mySelectBox").focusin(function () {
$(this).data["valueOnFocused"] = $(this).val();
});
$("#mySelectBox").keyup(function (e) {
if (e.which === 13)
{
doSomething($(this));
}
});
$("#mySelectBox").click(function () {
doSomething($(this));
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,当用户做出选择然后离开输入(无论是选择、文本框、单选按钮等)时,onchange 事件应该被触发。由于这在 IE 中不起作用,您可以尝试使用 onblur 来代替,以检测用户何时实际离开框。此时,您可以读取选择了哪个项目并采取相应的操作。这更多的是一种解决方法,但可能会满足您的需要。
编辑:另一种选择是检测 Enter 键的按下,如下所示:
characterCode 变量现在具有按下哪个按钮的“代码”。如果是回车键,则该代码将为 13。您可以听一下。
Basically the onchange event is supposed to be fired when the user makes a selection then leaves the input (be it select, textbox, radio button, whatever). Since this isn't working in IE, you could try using onblur instead, to detect when the user actually leaves the box. At that point you could read which item is selected and act accordingly. This is more of a workaround, but might do what you need.
Edit: another option would be to detect the pressing of the Enter key, like so:
The characterCode variable now has the "code" of which button was pressed. If it was the enter key, that code will be 13. You could listen for this.