jQueryUI 自动完成 - 无法显示返回的选择 (Coldfusion)
jQueryUI autocomplete - unable to display returned selection (Coldfusion)
这让我发疯。我正在使用 jQueryUI 自动完成来提取客户名称和客户 ID 对。用户应该能够开始输入客户名称并获得建议列表,select 从列表中,selection 后客户 ID 应分配给隐藏字段。自动完成在分配值 selected 之前工作正常。用户可以开始输入并获得建议列表和 select 一个。此时文本字段中的值将更改为客户ID而不是客户名称,简而言之就是问题。
jQuery代码:
$( "#enaCustomer" ).autocomplete({
source: "customerLookup.cfc?method=lookupCustomer&returnformat=json",
minLength: 2,
select: function(event, ui) {
console.log(ui);
console.log(ui.item.label);
$('#enaCustomer').val(ui.item.label);
$('#enaCusID').val(ui.item.value);
}
});
氟氯化碳:
<cffunction name="lookupCustomer" access="remote" output="no" returnformat="JSON">
<cfargument name="term" required="true" default="" />
<!--- Define variables --->
<cfset var returnArray = ArrayNew(1)>
<!--- Do search --->
<cfquery name="rsCustomers" datasource="myDB">
SELECT cusID, cusCustomer
FROM tblCustomers
WHERE cusCustomer LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#term#%" />
ORDER BY cusCustomer
</cfquery>
<!--- Build result array --->
<cfloop query="rsCustomers">
<cfset customerStruct = structNew() />
<cfset customerStruct['value'] = cusID />
<cfset customerStruct['label'] = cusCustomer />
<cfset arrayAppend(returnArray, customerStruct) />
</cfloop>
<!--- And return it --->
<cfreturn returnArray />
</cffunction>
console.log(ui)
显示此示例客户:
项目:对象{标签:"Customer 4",价值:4}
console.log(ui.item.label)
:
"Customer 4"
但这似乎不起作用:
$('#enaCustomer').val(ui.item.label);
一定有一些简单的东西我在这里遗漏了,但我不知道它是什么。有什么想法吗?
查看演示:jQuery UI | AutoComplete | Custom Data
建议如下:
$("#enaCustomer").autocomplete({
source: "customerLookup.cfc?method=lookupCustomer&returnformat=json",
minLength: 2,
select: function(event, ui) {
$('#enaCustomer').val(ui.item.label);
$('#enaCusID').val(ui.item.value);
return false;
}
});
这会导致函数结束而不执行任何进一步的操作。
这让我发疯。我正在使用 jQueryUI 自动完成来提取客户名称和客户 ID 对。用户应该能够开始输入客户名称并获得建议列表,select 从列表中,selection 后客户 ID 应分配给隐藏字段。自动完成在分配值 selected 之前工作正常。用户可以开始输入并获得建议列表和 select 一个。此时文本字段中的值将更改为客户ID而不是客户名称,简而言之就是问题。
jQuery代码:
$( "#enaCustomer" ).autocomplete({
source: "customerLookup.cfc?method=lookupCustomer&returnformat=json",
minLength: 2,
select: function(event, ui) {
console.log(ui);
console.log(ui.item.label);
$('#enaCustomer').val(ui.item.label);
$('#enaCusID').val(ui.item.value);
}
});
氟氯化碳:
<cffunction name="lookupCustomer" access="remote" output="no" returnformat="JSON">
<cfargument name="term" required="true" default="" />
<!--- Define variables --->
<cfset var returnArray = ArrayNew(1)>
<!--- Do search --->
<cfquery name="rsCustomers" datasource="myDB">
SELECT cusID, cusCustomer
FROM tblCustomers
WHERE cusCustomer LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#term#%" />
ORDER BY cusCustomer
</cfquery>
<!--- Build result array --->
<cfloop query="rsCustomers">
<cfset customerStruct = structNew() />
<cfset customerStruct['value'] = cusID />
<cfset customerStruct['label'] = cusCustomer />
<cfset arrayAppend(returnArray, customerStruct) />
</cfloop>
<!--- And return it --->
<cfreturn returnArray />
</cffunction>
console.log(ui)
显示此示例客户:
项目:对象{标签:"Customer 4",价值:4}
console.log(ui.item.label)
:
"Customer 4"
但这似乎不起作用:
$('#enaCustomer').val(ui.item.label);
一定有一些简单的东西我在这里遗漏了,但我不知道它是什么。有什么想法吗?
查看演示:jQuery UI | AutoComplete | Custom Data
建议如下:
$("#enaCustomer").autocomplete({
source: "customerLookup.cfc?method=lookupCustomer&returnformat=json",
minLength: 2,
select: function(event, ui) {
$('#enaCustomer').val(ui.item.label);
$('#enaCusID').val(ui.item.value);
return false;
}
});
这会导致函数结束而不执行任何进一步的操作。