我有一个表单字段,我使用coldfusion查询中的数据列表动态填充:
<input size="50" name="customer_name" id="customer_name"
tabindex="0" onblur="checkFilled()" type="text">
<datalist id=customers >
<cfloop query="past_customers">
<option>#past_customers.company_name#
</cfloop>
</datalist>
</cfoutput>
然后,我将根据该选择填充第二个表单字段:
<cftextarea name="customer_address"
cols="50" rows="5"
id="customer_address"
tabindex="0"
bind="cfc:cfcs.addressdata.getAddressDetail({customer_name})"
bindattribute="value"
bindonload="true"></cftextarea>
这是我的CFC代码:
<cffunction name="getAddressDetail" access="remote" returnType="string">
<cfargument name="customer_name" type="any" required="true">
<!--- localize function variables --->
<cfset var dataDetail = "">
<cfquery name="dataDetail" datasource="#datasource#">
SELECT company_address
FROM closed_tickets
WHERE company_name = <cfqueryparam value="#ARGUMENTS.customer_name#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfreturn dataDetail.company_address>
</cffunction>
<cffunction name="getEmailAddressDetail" access="remote" returnType="string">
<cfargument name="customer_name" type="any" required="true">
<!--- localize function variables --->
<cfset var dataDetail = "">
<cfquery name="dataDetail" datasource="#datasource#">
SELECT email
FROM closed_tickets
WHERE company_name = <cfqueryparam value="#ARGUMENTS.customer_name#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfreturn dataDetail.email>
</cffunction>
<cffunction name="getPhoneDetail" access="remote" returnType="string">
<cfargument name="customer_name" type="any" required="true">
<!--- localize function variables --->
<cfset var dataDetail = "">
<cfquery name="dataDetail" datasource="#datasource#">
SELECT phone
FROM closed_tickets
WHERE company_name = <cfqueryparam value="#ARGUMENTS.customer_name#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfreturn dataDetail.phone>
</cffunction>
这是按设计工作的,但我想尝试使用AJAX填充我的初始输入字段。 HEre是我正在使用的代码:
<!---Autocomplete for Quick view text box --->
<script>
$(document).ready(function() {
$( "##customer_name" ).autocomplete({
source: "cfcs/past_customers.cfc?method=lookupCustomers&returnformat=json",
minLength: 1,
select: function(event, ui) {
$('##customer_name').val(ui.item.value);
}
});
});
</script>
新的CFC:
<cffunction name="lookupCustomers" access="remote" output="no" hint="I return a list of customers" returnformat="JSON">
<cfargument name="term" required="false" default="" />
<!--- Define variables --->
<cfset var returnArray =ArrayNew(1)>
<!--- Do search --->
<cfquery name="data" datasource="#datasource#">
select distinct company_name
From closed_tickets
where ticket_type = "billable"
AND company_name LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.term#%" />
order by company_name
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset titleStruct = structNew() />
<cfset titleStruct['value'] = company_name />
<cfset titleStruct['label'] = company_name />
<cfset arrayAppend(returnArray,titleStruct) />
</cfloop>
<!--- And return it --->
<cfreturn returnArray />
</cffunction>
</cfcomponent>
我可以使用我的初始输入框来显示列表,但是在使用AJAX结果时,相关的输入字段不会填充数据。使用coldfusion LIST代码时,所有内容都将正确填充。我保留了所有字段名称。
我在哪里错了?