可以更改"jquery UI自动完成"功能中传递的默认"术语"名称吗?

Joh*_*Doe 5 javascript jquery jquery-ui autocomplete jquery-ui-autocomplete

我试图更改默认设置为jquery ui自动完成功能的"term"字段.是否可以轻松地将其更改为"q"(查询)而无需在"核心"文件中更改它?

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> 
Run Code Online (Sandbox Code Playgroud)

And*_*ker 17

是的,可以通过制作自己的AJAX请求来实现.

假设您有以下设置:

$("#myfield").autocomplete({
    source: '/my_url/myservice.xyz'
});
Run Code Online (Sandbox Code Playgroud)

默认情况下自动完成(如您所见)发送的请求如下所示:

myservice.xyz?term=abc"

您可以提供source自动完成选项的功能参考.在该函数内部,您可以创建自己的AJAX请求,如下所示:

$("#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([]);
             }
         });
     });
});
Run Code Online (Sandbox Code Playgroud)

这应该生成一个更像是的请求:

myservice.xyz?q=abc