jQuery 自动完成防止首次输入?
我有以下代码 - http://jsfiddle.net/vcvsb/1/ -
$(document).ready(function(e) {
$("input#autocomplete").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
jQuery("#autocomplete").keypress(function (e) {
if (e.keyCode == 13) {
$('#buttontest').trigger('click');
alert('crap, clicked');
}
});
});
我是尝试将其设置为用户可以使用箭头键向下移动 - 按 Enter 键,然后继续输入。我面临的问题 - 是一旦用户在获得自动完成响应后选择输入 - 它就会触发按钮单击?
我如何设置它,以便:
- 用户可以使用箭头键移动到选择,然后按回车键
- 在按回车键后,用户可以继续键入
- 如果用户再次按回车键,则会触发单击
?
I have the following code - http://jsfiddle.net/vcvsb/1/ - per
$(document).ready(function(e) {
$("input#autocomplete").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
jQuery("#autocomplete").keypress(function (e) {
if (e.keyCode == 13) {
$('#buttontest').trigger('click');
alert('crap, clicked');
}
});
});
I am trying to set it up so that the user can move down with the arrow keys - hit enter and then keep typing. The problem I am facing - is that as soon as the user selects enter after getting an auto-complete response - it triggers the button click ?
How can I set it up so that:
- A user can use the arrow keys to move to a selection, then hit enter
- After hitting enter, the user can keep typing
- If the user hits enter again, it triggers the click
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很好的一种破解,但有效 http://jsfiddle.net/amantur/hhUdV/ 。您需要记录单击 ENTER 的次数。当第二次点击触发按钮时。
well sort of a hack but works http://jsfiddle.net/amantur/hhUdV/ . You need to keep count of how may times ENTER is clicked. When it is second time trigger button click.
最好的解决方案是检查是否有焦点位于任何输入字段上,否则忽略 ENTER。使用 jQuery >= 1.6,您可以执行以下操作:
这将忽略除您想要的事件之外的任何其他 ENTER 事件;)
The best solution is to check if there's focus on any of your input fields, and otherwise ignore ENTER. With jQuery >= 1.6 you can do the following:
This will ignore any other ENTER event than the one you want ;)