如何使用jquery自动完成功能获取不在自动完成列表中的文本?

时间:2013-02-09 15:06:06

标签: jquery jquery-ui

   var options = {
       select: function(event,ui) {

         searchIndex = $.inArray(ui.item.value, arrayA)
   },
   source: function(req, response) {
        var re = $.ui.autocomplete.escapeRegex(reg, term);
        var matcher = new RegExp("^" + re + "i");
        response($.grep(arrayA, function(item, index) {
        return matcher.test(item);
    })); 
  }

};

鉴于上面列出的自动填充选项,如何在输入回车键后输入数组列表(arrayA)中未列出的文本,但在输入文本区域中输入?

  <input type=text id="searchAText" />

1 个答案:

答案 0 :(得分:0)

是的,这是个老话题。但我也有需要找出列表外的值。

当我使用 change 事件而不是 select 事件时我成功了,并检查了 ui.item 是否为 null 以及它的值是否在 $(this).val() 中。

所以在你的情况下,大概是这样的:

change: function(event,ui) {
    if (ui.item == null)
    searchIndex = $.inArray($(this).val(), arrayA);
    else
    searchIndex = $.inArray(ui.item.value, arrayA);
},