最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can the default "Term" name passed in the "jquery UI autocomplete" feature be c

programmeradmin1浏览0评论

I am trying to change the "term" field that is set to that by default with the jquery ui autocomplete feature. Is it possibly to easily change it to "q" (query) without going and changing it in the "core" file?

JavaScript:

<script>
    $(function() {
        $( "#spotify_song_search" ).autocomplete({
            source: ".json",
            data: { 
                q: request.term 
            },
            dataType: "getjson",
            minLength: 3,
            select: function( event, ui ) { 
                alert('select'); 
            }
        });
    });
</script> 

I am trying to change the "term" field that is set to that by default with the jquery ui autocomplete feature. Is it possibly to easily change it to "q" (query) without going and changing it in the "core" file?

JavaScript:

<script>
    $(function() {
        $( "#spotify_song_search" ).autocomplete({
            source: "http://ws.spotify.com/search/1/track.json",
            data: { 
                q: request.term 
            },
            dataType: "getjson",
            minLength: 3,
            select: function( event, ui ) { 
                alert('select'); 
            }
        });
    });
</script> 
Share Improve this question edited Jun 17, 2014 at 14:43 DarkAjax 16.2k11 gold badges57 silver badges66 bronze badges asked May 29, 2012 at 1:03 John DoeJohn Doe 3,67115 gold badges64 silver badges112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

Yes, it's possible by making your own AJAX request.

Assume you have the following setup:

$("#myfield").autocomplete({
    source: '/my_url/myservice.xyz'
});

Autocomplete by default (as you noticed) sends requests that look like:

myservice.xyz?term=abc"

You can supply a function reference to the source option of autocomplete. Inside that function you can make your own AJAX request, which would look like this:

$("#myfield").autocomplete({
     source: function (request, response) {
         // request.term is the term searched for.
         // response is the callback function you must call to update the autocomplete's 
         // suggestion list.
         $.ajax({
             url: "/my_url/myservice.xyz",
             data: { q: request.term },
             dataType: "json",
             success: response,
             error: function () {
                 response([]);
             }
         });
     });
});

This should generate a request looking more like:

myservice.xyz?q=abc

You could use the callback source option and make your own request.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论