jquery-ui自动完成不会在输入时选择 [英] jquery-ui autocomplete does not select on enter

查看:100
本文介绍了jquery-ui自动完成不会在输入时选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用jquery-ui自动完成输入框并设置了所选项目的隐藏值.

I have used the jquery-ui to autocomplete an input box and set a hidden value from the selected item.

我确实使用

 select: function(event, ui) { ...$("#myDiv").val(ui.item.value)... } 

选项(现在可能是错误的,手头没有代码,但是直到我提出问题为止,它才起作用...)

option (might be wrong now, don't have the code at hand, but it works up until my question...)

当使用鼠标从菜单中选择一个项目时,它可以工作,但是,如果我只是输入一些文本并使用Enter选择一个项目-它什么也没做,就好像自动完成功能完全没有执行选择功能.但是,立即使用选项卡触发选择.

It works when select an item from the menu with the mouse, however if I just enter some text and choose an item with Enter - it does not do anything, it is as if the select is not run at all by the autocomplete function. However, tabbing out of the box triggers the select.

我使用了焦点和更改:还更新了我想要的字段,但是我认为这过于矫kill过正,如果确实需要指定所有焦点,更改和选择,只是为了确保用户选择,从列表中选择一个项目.

I have used a focus and change: to also update the fields I want, but I think this is overkill, should it really be necessary to specify all of focus, change and select, just to be sure that however a user selects an item from the list it will actually be selected.

谢谢.

rofly:我正在使用jquery-ui自动完成功能,它具有您提供的代码,但是看起来像这样(来自jquery.ui.autocomplete.js)

rofly: I am using the jquery-ui autocomplete, it has the code you give, but it looks like this (from the jquery.ui.autocomplete.js)


case keyCode.ENTER:
                case keyCode.NUMPAD_ENTER:
                    // when menu is open or has focus
                    if ( self.menu.active ) {
                        event.preventDefault();
                    }
                    //passthrough - ENTER and TAB both select the current element
                case keyCode.TAB:
                    if ( !self.menu.active ) {
                        return;
                    }
                    self.menu.select( event );
                    break;

这看起来很花哨,所以我不确定是否因此而失败.

That looks all dandy, so Im not sure if it fails because of this.

我的代码是这样的(包装在document.read()

My code is like this (wrapped in document.read()

$("#someDiv").attr("autocomplete", 'off');
$("#someDiv").autocomplete({
source: function(request, response) {
if ( request.term in cache ) {
response( cache[ request.term ] );
return;
}
$.ajax({
url: "giveMeJSON.jsp",
dataType: "json",
data: request,
success: function( data ) {
cache[ request.term ] = data;
response( data );
}
})},
minLength: 1,
delay: 300,
select: function(event, ui) {
$("#someDiv").val(ui.item.label);
$("#hiddenDiv").val(ui.item.value);
}
});

因此,问题是,这个作品当用户从菜单中选择与鼠标和字段的跳格出时(使用keyUp,的keyDown选择然后选项卡时,作品),但使用keyUp,的keyDown选择ITME,然后进入,什么也没发生!

so, the problem is, this works when the user is selecting from the menu with the mouse AND when tabbing out of the field (keyUp,keyDown to choose then tab out, works) but keyUp,keyDown to choose itme, then enter, and nothing happens!

推荐答案

一个类似的实例适用于jQuery 1.4.2和jQuery-ui 1.8.7.请注意,仅在从列表中选择一个项目时,Enter才起作用.如果要创建不存在的新条目,则按Enter不会触发选择或更改事件.我必须绑定一个单独的事件处理程序才能使这种情况发生.

A similar instance works for me with jQuery 1.4.2 and jQuery-ui 1.8.7. One caveat, Enter only appears to work when selecting an item from the list. If you are creating a new entry that does not exist, pressing Enter will not trigger select or change events. I had to bind a separate event handler to make that situation fly.

这篇关于jquery-ui自动完成不会在输入时选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆