如何检测鼠标单击,在击倒中的下拉列表框中输入键或选项卡式项目 [英] how to detect a mouse click, enter key or tabbed item on drop down list box in knockout

查看:76
本文介绍了如何检测鼠标单击,在击倒中的下拉列表框中输入键或选项卡式项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户执行以下操作之一时,从下拉列表框中获取选定项目值的最佳方法是什么?

What is the best way to get the selected item value from a dropdown list box when the user does one of the following;

在项目上按Tab键

按Enter键或

鼠标单击该项目.

我是否必须为每个事件创建一个javascript事件处理程序,还是有一种很好的方法来实现剔除?

Do I have to create a javascript event handler for each event or is there a good way to do it with knockout.

有没有我可以看的好的jsfiddle示例?

Are there any good jsfiddle examples I could look at?

谢谢

推荐答案

您可以使用捕获这些事件的自定义绑定.

You could use a custom binding that catches those events.

ko.bindingHandlers.tabEnterClick = {
    init: function(element, valueAccessor) {
        $(element).click(function() {
            valuAccessor()();
        }).keydown(function(event) {
            if (event.which == 13 /*enter*/ || event.which == 9 /*tab*/) {
                valuAccessor()();
            }
        }
    }
};

但是,如果您只是想从下拉列表中了解选定的项目,那么value绑定就可以了.

But if you just want to know the selected item from a dropdown, the value binding does that just fine.

这篇关于如何检测鼠标单击,在击倒中的下拉列表框中输入键或选项卡式项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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