输入时自动完成(但自动选择时不自动完成)
我有一个简单的输入字段
,输入后我想提交它(它所在的表单)。我使用的是 jQuery,但没有使用任何 autoComplete 扩展,因此它只是默认浏览器扩展:
$('#searchfield')
.keypress(function(event) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (event)
keycode = event.which;
else
return true;
if (keycode == 13)
$('#searchForm').submit();
});
此解决方案存在问题: 当某人(在 FF3 中)开始输入(例如“stackov”),然后向下箭头选择所提供的选项并按 Enter 键时,表单将被发送出去,尽管搜索的是“stackov”,而不是所选的“stackoverflow”。
或者我尝试
$('#searchfield')
.change( function() {
$('#singleSearch').trigger('click');
})
这让我正确选择了“stackoverflow”,但现在任何字段的离开(例如在blur()中,在点击一些字母后)都会不由自主地发送关掉它。
对此有什么(理想的测试;)建议吗?
I have a simple input field
and on enter I want to submit it (the form that it is in). I am using jQuery but not any autoComplete extension, so it's just the default browser one:
$('#searchfield')
.keypress(function(event) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (event)
keycode = event.which;
else
return true;
if (keycode == 13)
$('#searchForm').submit();
});
Problem with this solution:
When somebody (in FF3) starts typing (say "stackov"), then goes arrow-down for the offered choice and hits enter, the form is sent off, albeit searching for "stackov", not for the chosen "stackoverflow".
Alternatively I tried
$('#searchfield')
.change( function() {
$('#singleSearch').trigger('click');
})
That got me correct picking of "stackoverflow", but now any leaving of the field (as in blur(), after tapping some letters for example) involuntarily sent it off.
Any (ideally tested;) advice on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我只能说,在提交之前允许“消化”自动完成的一个小延迟
修复问题:
Hmm, all I can say is, that a small delay to allow 'digest' of auto-complete before submit
fixes the issue: