2011-04-09 64 views
2

我有下面的代碼,建立jQuery UI autocomplete如何檢索源回調中的jQuery UI自動完成?

$('input').autocomplete({ 
    source : function(request, response) { 
     $.getJSON('/path?c_ajax=1', { input_value: request.term }, function(return_data) { 
      if (/* check to see if data is valid */) { 
       response(return_data.suggestions); 
      } else { 
       $('input').field_suggest('option', 'disabled', true); 
       response(new Array); 
      } 
     }); 
    } 
}); 

我不知道是否有一些其他的方式來獲得自動完成,除了$('input')或類似的?我的推理是,我可能會在一頁上爲同一字段提供多個自動完成功能,並且我想確保我擁有正確的一個。

編輯:是否有沒有辦法獲得源內的jQuery自動完成而不使用選擇器來獲取原始輸入,因此自動完成?

回答

1

爲什麼不在元素中添加特定的類或id?像這樣:

$('input.hello') //<input class="hello" type="text" /> 
$('input.hello2') //<input class="hello2" type="text" /> 
$('input#unique') //<input id="unique" type="text" /> 

歡呼

2

如果它是重要的是自動完成的排序,你可以使用:eq選擇或.eq方法,讓他們中的一個在特定的(從零開始)像這樣的索引:

// first input on the page 
$("input:eq(0)").autocomplete("search", "foo"); 

// third input in <form id="form"> 
$("#form input").eq(2).autocomplete("search", "foo");