嘿伙计们当我在源代码末尾添加一个变量时,我试图弄清楚为什么autocomplete()
方法没有GET
。例如:
<script>
$(document).ready(function(){
var search_input;
$('#search').keyup(function(){
search_input = ($(this).val());
console.log(search_input);
});
$('#search').autocomplete({
source: "http://192.168.33.10/app_dev.php/search/query/" + search_input,
minLength: 2
});
});
</script>
<div class="ui-widget">
<label for="search">Search</label>
<input type="text" id="search" />
</div>
然而,如果我从源中移除+ search_input
,它会执行GET
,就像这样..
<script>
$(document).ready(function(){
var search_input;
$('#search').keyup(function(){
search_input = ($(this).val());
console.log(search_input);
});
$('#search').autocomplete({
source: "http://192.168.33.10/app_dev.php/search/query/",
minLength: 2
});
});
</script>
<div class="ui-widget">
<label for="search">Search</label>
<input type="text" id="search" />
</div>
答案 0 :(得分:0)
即使您正在更改search_input
,小部件也只会获取初始值为search_input
的网址(可能为空)。
这就是你需要的东西:
$('#search').autocomplete({
source: function (request, response) {
$.get("http://192.168.33.10/app_dev.php/search/query/" + request.term, response)
},
minLength: 2
});
答案 1 :(得分:0)
在您的代码中,autocomplete
函数只需要为source参数提供回调函数。 source: function( request, response ) {}
您可以按照Multiple remote suggestions on jQuery UI Autocomplete
的示例进行操作 <script type="text/javascript">
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#search" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "http://192.168.33.10/app_dev.php/search/query/" + extractLast( request.term ), {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
</script>
此示例中服务器返回的JSON结构如下:
[{"id":"1544","label":"Suggestion 1","value":"Suggestion 1"},
{"id":"3321","label":"Suggestion 2","value":"Suggestion 2"}]