简体   繁体   English

jQuery .change()事件不会从键盘上的选择列表中触发:如何覆盖它?

[英]jQuery .change() event doesn't fire on select list from keyboard: how can I override this?

The jQuery .change() event doesn't fire the second time the user types and presses Enter to select an item. jQuery .change()事件不会在用户第二次键入时触发,并按Enter键以选择项目。 It does fire the first time. 它第一次开火。

See: http://jsfiddle.net/CtXbU/ 请参阅: http//jsfiddle.net/CtXbU/

If you focus on the list and type AD and press Enter, [EDIT] the alert fires. 如果您专注于列表并键入AD并按Enter键,则[编辑]将触发警报。 But if you then type AG and press Enter, nothing happens. 但是,如果您输入AG并按Enter,则不会发生任何事情。 The alert only pops up when you click away from the list. 单击远离列表时,仅会弹出警报。

What do I do if I want to handle the user changing the list both with the mouse and the keyboard? 如果我想处理用户使用鼠标和键盘更改列表,我该怎么办?

I know I can use .keypress() to handle keyboard events, but it feels odd to have two separate sections of code both doing the same thing. 我知道我可以使用.keypress()来处理键盘事件,但是让两个独立的代码段做同样的事情感觉很奇怪。

Is there a single jQuery event that would handle both mouse and keyboard changes as soon as they occur? 有没有一个jQuery事件可以在它们发生时立即处理鼠标和键盘的变化?

Thanks! 谢谢!

** UPDATE ** **更新**

This might clarify my question: http://jsfiddle.net/Bybr2/2/ 这可能会澄清我的问题: http//jsfiddle.net/Bybr2/2/

// How can I create an event (or handler) that 
// (a) fires AS SOON AS keypress is invoked from user keyboard input
// (b) also fires AS SOON AS change is invoked from user mouse input
// (c) does not fire a second time when change is invoked after keypress, when the user clicks away following keypress?

change after keypress can be very confusing - it only occurs when the user presses Enter or clicks away, so if I'm making client-side changes then, the user may well be very surprised! 按键后的change可能非常混乱 - 只有当用户按下Enter或点击时才会发生,所以如果我进行客户端更改,那么用户可能会非常惊讶!

The only solution I can think of is some kind of variable like user_has_just_done_keypress which is set as true on keypress and then reset to false on the next change event, but it feels very messy. 我能想到的唯一解决方案是某种变量,如user_has_just_done_keypress ,在user_has_just_done_keypress上设置为true ,然后在下一次change事件时重置为false ,但感觉非常混乱。 It also wouldn't work if you had a change from the mouse before the change from the user refocussing. 它如果你有一个同样是行不通的change从鼠标的前change从用户并重订。 Argh! 哎呀!

Add handler for keyup event, not keypress! keyup事件添加处理程序,而不是按键! If you use the same handler for keypress as for change, it does not work well in Firefox for example. 如果你使用与keypress相同的处理程序和更改,它在Firefox中不能很好地工作。

$("select").bind('change keyup', select_handler);

and handler 和处理程序

function select_handler() {
    $(this).val();
};

The change function would fire for keyboard entry for: 更改功能将触发键盘输入:

  • Google Chrome 21.0.1180.89 m 谷歌浏览器21.0.1180.89米
  • Internet Explorer 9.9.8112.16421 Internet Explorer 9.9.8112.16421

However not for Firefox 10.0.7 (All on Windows 7 Professional, SP1) 但不适用于Firefox 10.0.7(所有在Windows 7 Professional,SP1上)

Adding: 添加:

$('select').keypress(function() {
  $(this).change();
});

to the $(document).ready-function solved if for me, without breaking it for the other browsers. 到$(document).ready-function解决了,如果对我来说,没有打破其他浏览器。

The change event would still be triggered only once (in spite of the forced change event from within the keypress handler). 更改事件仍将仅触发一次(尽管来自按键处理程序中的强制更改事件)。

You can add keypress and have both function call another function doing your actual operation. 您可以添加keypress并让两个函数调用另一个函数来执行您的实际操作。 This way the duplication is not as bad. 这样复制就不那么糟了。 Actually the enter key fires the event in firefox 实际上,回车键在firefox中触发事件

That's how .change() works for text inputs. 这就是.change()适用于文本输入的方式。 You must focus out for the change to take effect. 您必须专注于更改才能生效。 That is also documented at http://api.jquery.com/change/ 这也记录在http://api.jquery.com/change/

The change() Api documentation reads change()Api文档读取

...the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus. ...当用户使用鼠标进行选择时,会立即触发事件,但对于其他元素类型,事件将延迟,直到元素失去焦点。

UPDATE You can combine both keypress and change in one live function as below 更新您可以将按键和更改组合在一个实时功能中,如下所示

$('div').live({
  'change': function() {},
  'keypress': function() {}
})

I've seen this before when working with IE (all versions, I think). 我在使用IE之前已经看过这个(所有版本,我认为)。 Change events from the keyboard will only fire when the input loses focus; 只有输入失去焦点时才会触发键盘更改事件; binding to multiple events was the only way to get the handler working like you expect it to. 绑定到多个事件是使处理程序像您期望的那样工作的唯一方法。 You can kill some of the redundancy by defining the handler as a global function, and then setting it as the handler for multiple events: 您可以通过将处理程序定义为全局函数来杀死一些冗余,然后将其设置为多个事件的处理程序:

function change_handler()
{
    // ...stuff...
}

$(document).ready(function() {
    $('input.whatever').bind('change keypress other_event ...', change_handler);
});

Not as clean as I'd like it to be, but it's not that bad, and it works. 不像我想的那样干净,但它并没有那么糟糕,而且它有效。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM