2011-06-09 48 views
0

全部Jquery Auto完成重點丟失

我正在使用JQuery自動完成。這真的很棒。 我需要您的幫助,以更好的方式爲我的需求使用這個組件。

問題:

1.使用向上和向下箭頭鍵可以自動完成列表框中導航。當我到達最後時,我的意思是當焦點位於列表中的最後一個項目時,如果按下箭頭鍵,焦點不會立即到達第一個元素,並且存在一些延遲(一個鍵延遲) 。我該如何解決這個問題?

PLZ看到以下站點

http://jqueryui.com/demos/autocomplete/

類型字符 'a' 在文本框中顯示給出的演示。

一旦列表中的最後一項(斯卡拉)集中。現在,如果我按下箭頭鍵,則第一項即ActionScript不會突出顯示(我的意思是不重點)立即。

所以,我的查詢是

1.Is有立即聚焦第一項(後最後一個項目)沒有任何延遲的任何方式?

2.是否可以限制焦點事件(以便我可以在最後一個項目結束時總結最後一個項目)。

回答

1

如果要自動選擇選項,看看這個網站:http://github.com/scottgonzalez/jquery-ui-extensions

我使用這個代碼(JSON結果):

jQuery.fn.extend({ 
CityAC: function (selectorOrCountry) { 
    return this.each(function() { 
     var el = $(this); 
     var id = el.attr("id"); 
     var md = el.metadata(); 
     var parentEl = $('#' + md.parentId); 

     el.autocomplete({ 
      source: function (request, response) { 
       var selectedCountry = ""; 
       if (selectorOrCountry !== undefined && selectorOrCountry !== null) { 
        if ($(selectorOrCountry).size() > 0) { 
         selectedCountry = $(selectorOrCountry).val(); 
        } else { 
         selectedCountry = selectorOrCountry; 
        } 
       } 
       $.ajax({ 
        url: '/Jx/GetCities', 
        type: 'post', 
        dataType: 'json', 
        data: { text: request.term, country: selectedCountry }, 
        success: function (data) { 
         if (data != null) { 
          response($.map(data, function (item) { 
           return { 
            label: item.Name, 
            value: item.Code 
           } 
          })); 
         } 
        } 
       }); 
      }, 
      select: function (event, ui) { 
       el.val(ui.item.label); 
       parentEl.val(ui.item.value); 
       return false; 
      }, 
      minLength: 2 
     }).data('autocomplete')._renderItem = function (ul, item) { 
      var templateItem = $('<a></a>'); 

      templateItem.append('<img src="/Assets/images/icon_town.png" width="16" align="absmiddle" class="ui-menuitem-icon" />'); 
      templateItem.append(item.label); 

      return $('<li></li>') 
      .data('item.autocomplete', item) 
      .append(templateItem) 
      .appendTo(ul); 
     }; 

     if (md.label !== undefined && md.label !== null) { 
      el.val(md.label); 
      parentEl.val(md.value); 
      parentEl.data(md.country); 
     } 
    }); 
} 
}); 


$('#inputID').CityAC(); 
+0

嗨,謝謝你的回覆。建議的代碼對於自動選擇和自動對焦非常有用。但是我的實際意圖是問這個問題是:重點應該列在列表中最後一項之後的列表中的第一項。但現在實際上並沒有立即出現,從最後一個項目移動到第一個項目會有一個關鍵延遲。你能以這種方式建議嗎?謝謝你 – Cherry 2011-06-09 12:38:59

0

此代碼覆蓋標準ui.autocomplete方法:

$.ui.autocomplete.prototype._move = function(direction, event) { 

     if (!this.menu.element.is(":visible")) { 
      this.search(null, event); 
      return; 
     } 
     if (this.menu.isFirstItem() && /^previous/.test(direction)) 
     { 
      this._value(this.term); 
      this.menu._move("first", "first", event); 
     } 
     else if (this.menu.isLastItem() && /^next/.test(direction)) 
     { 
      this._value(this.term); 
      this.menu._move("last", "last", event); 
     } 

     this.menu[ direction ](event); 
    };