开发者

Changing Query Parameter for jQuery UI's Autocomplete

开发者 https://www.devze.com 2023-03-19 17:24 出处:网络
I\'m usin开发者_开发问答g version 1.8.13 of jQuery UI\'s Auto complete and by default jQuery is using the query parameter of \"?term=\" by default, while my app is using \"?q=\" in the string it is cr

I'm usin开发者_开发问答g version 1.8.13 of jQuery UI's Auto complete and by default jQuery is using the query parameter of "?term=" by default, while my app is using "?q=" in the string it is creating. I COULD change the variable to be "term" on the backend, but I'd rather just tell jQuery what the server is trying to send it. Is there a way to change this?

Right now I just have something like this and it works if I change the variable to "term" on the backend, but like I said I wanted to change it to "q" and I can't find any info online about setting the parameter (that works):

$( "#input-search").autocomplete({
   source: "/search/autocomplete/"
});


You could use the callback form of source and handle all the interaction with the server yourself. Something like this:

$("#input-search").autocomplete({
    source: function(request, response) {
        $.get('/search/autocomplete', { q: request.term }, function(data) {
            response(data.split('\n'));
        });
    }
});


The answer above is a great simple example and helped me get to this result on my app which generates an unordered list from the response data which displays the autocomplete results:

$("#input-search").autocomplete({
   source: function(request, response) {
      $.ajax({
         url: "/search/autocomplete",
         dataType: "json",
         data: {
            q: request.term
         },
         success: function(data) {
            response(data);
         }
      });
   },
}).data("autocomplete")._renderItem = function(ul, item) {
    $(ul).attr('id', 'search-autocomplete');
       return $("<li class=\""+item.type+"\"></li>")
       .data( "item.autocomplete", item )
       .append("<a href=\""+item.url+"\">"+item.title+"</a>").appendTo(ul);
    };
0

精彩评论

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