键盘滚动时不触发Jquery列表框更改事件 [英] Jquery Listbox Change event does not fire on Keyboard scrolling

查看:45
本文介绍了键盘滚动时不触发Jquery列表框更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML表单上有一个简单的列表框和这个非常基本的jQuery代码

I've got a simple Listbox on a HTML form and this very basic jQuery code

    //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?

推荐答案

onchange 事件通常不是直到元素失去焦点为止。您还需要使用 onkeypress 。可能类似于:

The onchange event isn't generally fired until the element loses focus. You'll also want to use 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);

你需要 onchange onkeypress 分别考虑鼠标和键盘交互。

You'll want both onchange and onkeypress to account for both mouse and keyboard interaction respectively.

这篇关于键盘滚动时不触发Jquery列表框更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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