how to detect a mouse click, enter key or tabbed item on drop down list box in knockout

[亡魂溺海] 提交于 2019-12-10 22:33:13

问题


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

hits the tab key on the item,

presses the enter key or

mouse clicks the item.

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

Are there any good jsfiddle examples I could look at?

thanks


回答1:


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()();
            }
        }
    }
};

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



来源:https://stackoverflow.com/questions/15258367/how-to-detect-a-mouse-click-enter-key-or-tabbed-item-on-drop-down-list-box-in-k

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!