jquery自动完成行为修改

时间:2011-10-19 12:54:24

标签: jquery jquery-ui-autocomplete

我终于有了一些自动填充功能,但有一种我想改变的行为。如果用户键入部分值并单击返回列表之外的任何位置,则会在输入中设置部分值。如果用户没有选择其中一个返回的选项,我希望该值保持空白。

看起来焦点事件是我应该修改的,但我发现API令人困惑......它说:

Before focus is moved to an item (not selecting), ui.item refers to the focused 
item. The default action of focus is to replace the text field's value with the 
value of the focused item, though only if the focus event was triggered by a 
keyboard interaction. Canceling this event prevents the value from being updated,
but does not prevent the menu item from being focused.

但它没有举例说明如何取消活动。

1 个答案:

答案 0 :(得分:1)

在另一个问题中找到了答案:)像这样修改变更事件似乎可以解决问题:

$('#myID').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "cfc/autoSuggest.cfc?method=someMethod&returnformat=json",
                    dataType: "json",
                    data: {
                      search: request.term,
                      maxRows: 20
                    },
                    success: function(data) {
                      response(data);
                    }                   
                })
            },
            change: function(event, ui) {     
                if (!ui.item) {
                    $(this).val('');
                }
            }
        });