【发布时间】:2014-12-31 21:24:11
【问题描述】:
好的,
我尝试了很多不同的方法,但我似乎无法弄清楚如何在输入搜索后使用 Tab 键来选择所选选项。回车键工作得很好。
我会展示我尝试过的东西,但老实说,我不知道从哪里开始。
这里是示例代码:
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.4.js'></script>
<script type='text/javascript' src="http://harvesthq.github.io/chosen/chosen.jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://harvesthq.github.io/chosen/chosen.css">
</head>
<body>
<select id="chosen_example" style="width: 200px;" multiple="multiple">
<option value="1">A English Test A</option>
<option value="1">B German Test B</option>
<option value="1">C Greek Test C</option>
</select>
<script>
$(document).ready(function () {
var chosen_control = $('#chosen_example');
chosen_control.chosen().keydown(function (e, obj) {
//it's not getting in here
console.log('key pressed');
if (e.which == 9) {
console.log('tab key pressed');
//not sure what to do at this point
}
});
});
</script>
</body>
</html>
我试过.chosen().bind(...)。我试过$('#chosen_example').bind(...),还有其他一些东西
这是随之而来的JS Fiddle。
任何帮助或指导将不胜感激,或者如果您需要更多信息,请发表评论。
编辑 - 解决方案
根据收到的答案,Wondercricket 的答案将我引向了正确的方向。
通过更改核心js文件choosen.jquery.js,我在case 9下添加了以下内容:
if (this.results_showing) {
this.result_select(evt);
}
this.mouse_on_container = false;
break;
我将此添加到Chosen.prototype.keydown_checker 和AbstractChosen.prototype.keyup_checker
【问题讨论】:
-
只是一个猜测,但你能用
chosen_control.focusout()来表示你已经离开了元素的焦点吗?我无法使用 jsfiddle 进行有效测试...
标签: jquery jquery-chosen