2011-04-09 43 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");