可以更改“jquery UI自动完成”function中传递的默认“术语”名称吗?
我正在尝试更改默认情况下使用jquery ui自动完成function设置的“term”字段。 是否可以轻松地将其更改为“q”(查询)而无需在“核心”文件中更改它?
JavaScript的:
$(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'); } }); });
是的,可以通过制作自己的AJAX请求来实现。
假设您有以下设置:
$("#myfield").autocomplete({ source: '/my_url/myservice.xyz' });
默认情况下自动完成(如您所见)发送的请求如下所示:
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([]); } }); }); });
这应该生成一个更像是的请求:
myservice.xyz?q=abc
您可以使用回调source
选项并发出自己的请求。