gpt4 book ai didi

javascript - jquery ui 自动完成没有返回数据

转载 作者:行者123 更新时间:2023-11-29 15:34:56 24 4
gpt4 key购买 nike

我在自动完成方面遇到了问题。我有以下代码;

<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>

$(function() {
$('.namesuggestclass').autocomplete({
source: function(request, response){
$.ajax({
url: 'cfc/basic.cfc?method=getIndivs',
dataType: "json",
data: {
searchterm: request.term
},
error: function(xhr, textStatus, errorThrown) {
alert(errorThrown);
},
success: function(data){
response(data);
}
})
},
minLength: 3,
onSearchError: function (query, jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
select: function(event, ui) {
alert(ui.item.ISRECNUM);
}
});
});



<cfinput name="namesuggest" class="namesuggestclass">

我在 Coldfusion 10 服务器上运行。当我输入 3 个或更多字符时,我得到 3 个空行。

我的 cfc 是:

<cffunction name="getIndivs" access="remote" HINT="Get All Resources" returnformat="json" output="false" >
<cfargument name="searchterm" required="True" default="">
<cfset local.returnArray =ArrayNew(1)>
<cfquery name="local.get_Indivs" datasource="#request.dsn#" maxrows=20>
SELECT ui.id as isrecnum, UI.lastname + ', ' + UI.firstname as iname , c.name as companyname, c.city as companycity, UI.Status_Flag
FROM users_info AS UI left join
companies as c on c.id = ui.current_company_number
WHERE 0=0
And ui.status_Flag != 'D'
<cfif trim(arguments.searchterm) NEQ "">
AND UI.lastname + ' ' + ui.firstname + ' ' + c.name like
<cfqueryparam cfsqltype="cf_sql_varchar" value="#left(trim(arguments.searchterm),255)#%">
<cfelse>
AND UI.lastname = 'abcdefg' <!--- return empty query vjl 2011/07/27 --->
</cfif>
ORDER BY lastname, firstname
</cfquery>
<cfloop query="local.get_Indivs">
<cfset indivStruct = structNew() />
<cfset indivStruct['ISRECNUM'] = ISRECNUM />
<cfset indivStruct['INAME'] = INAME />
<cfset indivStruct['COMPANYNAME'] = COMPANYNAME />
<cfset indivStruct['COMPANYCITY'] = COMPANYCITY />
<cfset arrayAppend(local.returnArray,indivStruct) />
</cfloop>
<CFRETURN local.returnArray>
<!--- <CFRETURN local.get_Indivs> --->
</cffunction>

cfc调用返回的数据是;[{"ISRECNUM":77137,"INAME":"Lan, Donald","COMPANYNAME":"Company A","COMPANYCITY":"Dallas"},{"ISRECNUM":240316,"INAME":"Lan, Mike","COMPANYNAME":"公司 B","COMPANYCITY":"卡尔加里"},等等…………]

编辑:我更新了我的代码和数据。我看不出我在做什么与工作示例有很大不同。

最佳答案

正如vilsad所说:json数据格式错误。您尝试重建从数据库中获取的数据,jqueryui-autocomplete 所需的 json 格式为:

label:xxxx  --display as select's title 
value:xxxx --as select element's value

在 php 中,我像这样格式化我的数据:

$ds=$db->query('select id,title from products whrere title like'.keyword.'%');
$result=array(
'label'=>$ds['title'],
'value'=>$ds['id']
);
echo json_encode($result);

原谅我的英语

关于javascript - jquery ui 自动完成没有返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com