简体   繁体   English

键盘滚动时不触发Jquery列表框更改事件

[英]Jquery Listbox Change event does not fire on Keyboard scrolling

I've got a simple Listbox on a HTML form and this very basic jQuery code 我在HTML表单上有一个简单的Listbox和这个非常基本的jQuery代码

    //Toggle visibility of selected item
    $("#selCategory").change(function() {
        $(".prashQs").addClass("hide");
        var cat = $("#selCategory :selected").attr("id");
        cat = cat.substr(1);
        $("#d" + cat).removeClass("hide");
    });

The change event fires fine when the current item is selected using the Mouse, but when i scroll through the items using the keyboard the event is not fired and my code never executes. 当使用鼠标选择当前项目时,更改事件会激活,但是当我使用键盘滚动项目时,事件不会被触发,我的代码永远不会执行。

Is there a reason for this behavior? 这种行为有原因吗? And what's the workaround? 什么是解决方法?

The onchange event isn't generally fired until the element loses focus. 在元素失去焦点之前,通常不会触发onchange事件。 You'll also want to use onkeypress . 你也想使用onkeypress Maybe something like: 也许是这样的:

var changeHandler = function() {
    $(".prashQs").addClass("hide");
    var cat = $("#selCategory :selected").attr("id");
    cat = cat.substr(1);
    $("#d" + cat).removeClass("hide");
}

$("#selCategory").change(changeHandler).keypress(changeHandler);

You'll want both onchange and onkeypress to account for both mouse and keyboard interaction respectively. 您将希望onchangeonkeypress分别考虑鼠标和键盘交互。

Sometimes the change behavior can differ per browser, as a workaround you could do something like this: 有时,每个浏览器的更改行为可能会有所不同,因为您可以执行以下操作:

 //Toggle visibility of selected item
    $("#selCategory").change(function() {
        $(".prashQs").addClass("hide");
        var cat = $("#selCategory :selected").attr("id");
        cat = cat.substr(1);
        $("#d" + cat).removeClass("hide");
    }).keypress(function() { $(this).change(); });

You can chain whatever events you want and manually fire the change event. 您可以链接所需的任何事件并手动触发更改事件。

IE: IE:

var changeMethod = function() { $(this).change(); };
....keypress(changeMethod).click(changeMethod).xxx(changeMethod);

The behavior you describe, the change event triggering by keyboard scrolling in a select element, is actually an Internet Explorer bug. 您描述的行为,即在select元素中通过键盘滚动触发的更改事件,实际上是Internet Explorer错误。 The DOM Level 2 Event specification defines the change event as this: DOM Level 2 Event规范将change事件定义为:

The change event occurs when a control loses the input focus and its value has been modified since gaining focus. 当控件失去输入焦点并且自获得焦点后其值已被修改时,会发生更改事件。 This event is valid for INPUT, SELECT, and TEXTAREA. 此事件对INPUT,SELECT和TEXTAREA有效。 element. 元件。

If you really want this behavior, I think you should look at keyboard events. 如果你真的想要这种行为,我认为你应该看一下键盘事件。

$("#selCategory").keypress(function (e) { 
  var keyCode = e.keyCode || e.which; 
  if (keyCode == 38 || keyCode == 40) { // if up or down key is pressed
     $(this).change(); // trigger the change event
  } 
}); 

Check a example here ... 点击此处查看示例...

$('#item').live('change keypress', function() { /* code */ });

I had this problem with IE under JQuery 1.4.1 - change events on combo boxes were not firing if the keyboard was used to make the change. 我在JQuery 1.4.1下遇到了IE的这个问题 - 如果键盘用于进行更改,则组合框上的更改事件不会触发。

Seems to have been fixed in JQuery 1.4.2. 似乎已在JQuery 1.4.2中修复。

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

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