开发者

How do I retrieve the jQuery UI autocomplete within a source callback?

I have the following code, setting up a jQuery UI autocomplete:

$('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);
            }
        });
    }
});

I'm wondering if there is some other way 开发者_开发问答to get the autocomplete, other than $('input') or similar? My reasoning is that I may have multiple autocompletes for the same field on 1 page and I want to make sure I have the right one.

Edit: Is there is no way of getting the jQuery autocomplete within the source without using the selectors to getting the original input and therefore the autocomplete?


If it is the ordering of the autocompletes that is important, you can use the :eq selector or the .eq method to get one of them at a specific (zero-based) index like this:

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

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


why not add a specific class or id into the element? like this:

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

cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜