Coldfusion 8 中的自动完成
我需要添加自动完成小部件以从数据库中搜索单词,即使字符位于单词的中间 这是我的 CFM 页面:
<script src="jquery-1.4.2.min.js"></script>
<script src="jquery-ui-1.8.custom.min.js"></script>
<link rel="stylesheet" href="jquery-ui-1.8.custom.css" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
$('#Names').autocomplete(
{source: function(request, response) {
$.ajax({
url: "cfc/getValues.cfc?method=getNames>&returnformat=json",
dataType: "json",
data: {
search: request.term,
maxRows: 10
},
success: function(data) {
response(data);
}
})
},
parse: function(data){
return $.map(data, function(item) {
return { data: item, value: item, result: item };
});
}
});
});
</script>
category: <input id="Names" />
这是 CFC 页面:
//cfc file getValues.cfc
<cffunction name="getNames" access="remote" returntype="String" >
<cfargument name="search" type="any" required="false" default="">
<cfset var data="">
<cfset var result=ArrayNew(1)>
<cfquery name="data" datasource="dbNAme">
SELECT NAME
FROM myTable
WHERE NAME LIKE '%#trim(ARGUMENTS.search)#%'
ORDER BY NAME
</cfquery>
<cfloop query="data">
<cfset returnStruct = StructNew() />
<cfset returnStruct["label"] = NAME />
<cfset ArrayAppend(result,returnStruct) />
</cfloop>
<cfreturn serializeJSON(result) />
</cffunction>
没有 javascript 错误,但我根本无法让它工作。当我在文本框中写入时,没有任何反应,我的代码中是否有问题?
I need to add the autocomplete widget to search for a word from the database,even if the characters are in the middle of the word
This My CFM page :
<script src="jquery-1.4.2.min.js"></script>
<script src="jquery-ui-1.8.custom.min.js"></script>
<link rel="stylesheet" href="jquery-ui-1.8.custom.css" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
$('#Names').autocomplete(
{source: function(request, response) {
$.ajax({
url: "cfc/getValues.cfc?method=getNames>&returnformat=json",
dataType: "json",
data: {
search: request.term,
maxRows: 10
},
success: function(data) {
response(data);
}
})
},
parse: function(data){
return $.map(data, function(item) {
return { data: item, value: item, result: item };
});
}
});
});
</script>
category: <input id="Names" />
and this the CFC page:
//cfc file getValues.cfc
<cffunction name="getNames" access="remote" returntype="String" >
<cfargument name="search" type="any" required="false" default="">
<cfset var data="">
<cfset var result=ArrayNew(1)>
<cfquery name="data" datasource="dbNAme">
SELECT NAME
FROM myTable
WHERE NAME LIKE '%#trim(ARGUMENTS.search)#%'
ORDER BY NAME
</cfquery>
<cfloop query="data">
<cfset returnStruct = StructNew() />
<cfset returnStruct["label"] = NAME />
<cfset ArrayAppend(result,returnStruct) />
</cfloop>
<cfreturn serializeJSON(result) />
</cffunction>
No javascript error but i can't get it to work at all.when i write in the textbox nothing happens is there something wrong in my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
您的搜索词是
如果我正确理解您的问题的整个单词,它应该是
注意已删除 % 通配符
但是您可能还会问问题“我的自动完成功能根本不起作用”在这种情况下,我将下载并安装 fiddler 因为这是一个很棒的调试工具,并且非常适合通过 AJAX 调用进行调试。
Your search term is
If I understand your question correctly for whole words it shoud be
Notice have removed the % wildcard
However you might also be asking the question "my autocomplete does not work at all" In this case I would download and install fiddler as this is a great debug tool and works brilliantly for debugging through AJAX calls.