所以我尝试传递另一个参数“field”,它可以让我获取正在输入的输入的 id。这将允许我按照我的意愿指导 PHP 对数据库发出正确的 SQL 语句页面上有多个需要自动完成才能工作的输入。我知道下面缺少回复,那是因为我不确定要放什么。我只想像平常一样返回 json 字符串。我怎样才能获得要传递的 id 以及我应该为响应添加什么内容?
('.completeme').autocomplete({
source: function(request, response) {
$.ajax({
url: "ajax/ajax_diagnosis.php",
datatype: "json",
data: {
term: request.term,
field: $(this).attr("id"),
maxRows: 15
}
},
minLength: 3,
});
最佳答案
尝试:
$(this.element).attr("id")
这可能是最终的脚本:
$(function() {
var cache = {},
lastXhr={};
$( ".completeme" ).autocomplete({
minLength: 3,
source: function( request, response ) {
var term = request.term,
field=$(this.element).attr('id');
if(!field)return;
if ( cache[field] && term in cache[field] ) {
response( cache[field][ term ] );
return;
}
$.extend(request,{field:field,maxRows:15});
lastXhr[field] = $.getJSON( "ajax/ajax_diagnosis.php",
request,
function( data, status, xhr ) {
if(!cache[field])cache[field]={};
cache[field][ term ] = data;
if ( xhr === lastXhr[field] ) {
response( data );
}
});
}
});
});
关于Jquery UI 自动完成这无法将 id 作为单独的参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032408/