【发布时间】:2016-05-27 14:37:22
【问题描述】:
我一直很难找到解决问题的方法。我使用 jQuery 创建了一个自动完成列表,其中有一个 focus 事件。当关注文本框时,用户可以看到建议列表。这是在focus 事件中处理的,并且工作正常。
现在表单上有多个其他输入设置了tabIndex,因此用户可以通过 tab 键在输入之间导航。如果用户通过 tab 键到达文本框,我不希望出现建议列表。
这里是短代码:
$('#tbprofession').autocomplete({
source: function(request, response) {
response($.map(professionList, function(value) {
if (value.Label.toLowerCase().startsWith(request.term.toLowerCase())) {
return {
label: value.Label,
vvalue: value.ID
};
}
}));
},
select: function(e, i) {
$("#tbprofession").val(i.item.label);
$scope.selectedprofession = i.item.vvalue;
return false;
},
minLength: 0,
scroll: true
}).bind('focus', function(e) { //here is the question
$(this).val(TAC_APPLICATION_CONSTANTS.COMMON.EMPTYSTRING);
$(this).autocomplete("search");
return false;
}});
编辑
如果我使用点击事件而不是焦点,我可以获得键码
}).bind('click', function(e) { //here is the question
**//how to get keycode here or will this method fire on tab press?**
}});
简而言之,如何区分标签焦点和手动焦点?
这是一个产生问题的小提琴http://jsfiddle.net/ubugC/321/
任何人都可以在这方面提供进一步的帮助。
【问题讨论】:
-
不确定“手动对焦”的用途。如果您的意思是仅单击该框,则可以使用
click事件,不是吗? -
感谢@GertG 的评论。如果您在代码中看到您的意思是
.bind('click',,对吗?也尝试过,但如果我按 Tab 键并到达文本框中,列表仍然会打开。跨度>
标签: jquery jquery-ui autocomplete