我有一个表单设置,我从列表中选择一个名称,调用 CFC 并针对该名称运行查询。然后,我从该查询返回一个美元金额。
然后我尝试获取该美元金额并更改 <input>
的值元素。
我可以成功运行 CFC 并返回正确的值,但我的 JQUERY 不会更改输入字段的值。它跳过我的 success: 方法并直接转到我的 error: 方法。
这是我的表单代码:
<cfselect class="required" queryPosition="below" query="get_ticket" display="company_name" name="customer_checkout" id="customer_checkout" tabindex="0" onchange="PopulateGrandTotal();" ><option>---Make A Selection---</option></cfselect>
<div id="grant_totalDIV" >
<input type="number" name="grand_total_due" id="grand_total_due">
</div>
这是我的 CFC:
<cffunction name="getTotal" access="remote" returntype="any">
<cfargument name="customer_checkout" type="any" required="true">
<!--- localize function variables --->
<cfset var dataDetail = "">
<cfquery name="dataDetail" datasource="#datasource#" >
select grand_total
from service_ticket
where company_name = '#customer_checkout#'
</cfquery>
<cfoutput query="dataDetail">
<cfreturn dataDetail.grand_total>
</cfoutput>
</cffunction></cfcomponent>
这是 JQuery:
<script>
function PopulateGrandTotal(){
// Populate the customer alert DIV based on the customer selection
console.log( $("#customer_checkout>option:selected").attr("Value") );
$.ajax({
url:'cfcs/grand_totalDIV.cfc?method=getTotal&returnformat=json',
dataType: 'json',
data: { customer_checkout: $("#customer_checkout>option:selected").attr("Value") },
success: function(response) {
console.log('Successfully ran JSON, now changing input value');
$("#grand_total_due").val( response );
console.log('Input value cahnged');
},
error: function(response){
console.log('Error');
}
});
}
</script>
我的 JSON 响应:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
274.00
有人可以帮忙吗?
最佳答案
看起来您只是返回文本,因此不要使用 dataType: 'json',
因为这不是发送的内容。
您可以从 ajax 配置中删除该选项
否则,您将需要 json 序列化结构并在成功处理程序中访问正确的属性
关于jquery - 使用 CFC JSON 结果填充输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35704787/