http://harvesthq.github.com/chosen/
这是示例页面,它具有多选输入。
当我开始打字时,它会突出显示选项。此时,当我按 Tab 按钮时,焦点将移至其他输入并将先前的输入留空。我想在用户开始输入时添加新选项,例如,用户输入“af”,突出显示“阿富汗”,用户按下选项卡按钮,必须添加“阿富汗”。
我尝试处理Tab键按下,并执行$(optionid).click()
来模拟鼠标或输入选择,但它不起作用
最佳答案
在 Chosen 之上添加事件处理程序时,我已经尝试了所有我能想到的方法,但没有成功。我尝试过捕获 Tab 键按下并模拟对突出显示的项目的点击,并模拟在输入字段上按 Enter 键,但无济于事。
但是我认为,如果您愿意破解 Chosen,只需要进行一些非常小的更改。
第一个变化:
Chosen.prototype.keydown_checker = function(evt) {
...
//Replace the existing "case 9:" with this:
case 9: //tab
if(!this.is_multiple){
if (this.results_showing && !this.is_multiple) {
this.result_select(evt);
}
this.mouse_on_container = false;
}
else {
evt.preventDefault();
}
break;
...
}
第二个变化:
AbstractChosen.prototype.keyup_checker = function(evt) {
...
case 9: //Simply add this above "case 13:" and remove "case 9:" from further down the switch statement
case 13: //Enter
...
}
编辑:我现在已经使用他们捆绑的示例页面对此进行了测试,它似乎有效。正如预期的那样。
关于Jquery选择: populate Multiple Select with Tab button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668221/