javascript 可以更改“jquery UI 自动完成”功能中传递的默认“术语”名称吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10791968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can the default "Term" name passed in the "jquery UI autocomplete" feature be changed?
提问by John Doe
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?
我正在尝试使用 jquery ui 自动完成功能更改默认设置的“术语”字段。是否可以轻松地将其更改为“q”(查询)而无需在“核心”文件中进行更改?
JavaScript:
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>
回答by Andrew Whitaker
Yes, it's possible by making your own AJAX request.
是的,可以通过发出您自己的 AJAX 请求来实现。
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"
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:
您可以提供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([]);
}
});
});
});
This should generate a request looking more like:
这应该会生成一个看起来更像的请求:
myservice.xyz?q=abc
myservice.xyz?q=abc
回答by Dave Newton
You could use the callback source
option and make your own request.
您可以使用回调source
选项并提出您自己的请求。