Coldfusion 8 中的自动完成

发布于 2025-01-07 01:34:31 字数 2135 浏览 1 评论 0原文

我需要添加自动完成小部件以从数据库中搜索单词,即使字符位于单词的中间 这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2025-01-14 01:34:31

试试这个:

<cfloop query="data">
    <cfset statesStruct = StructNew() />
    <cfset statesStruct["id"] = id />
    <cfset statesStruct["label"] = name />
    <cfset statesStruct["value"] = name />

    <cfset ArrayAppend(returnArray,statesStruct) />
</cfloop>

<cfoutput>
    #serializeJSON(returnArray)#
</cfoutput>

Try this:

<cfloop query="data">
    <cfset statesStruct = StructNew() />
    <cfset statesStruct["id"] = id />
    <cfset statesStruct["label"] = name />
    <cfset statesStruct["value"] = name />

    <cfset ArrayAppend(returnArray,statesStruct) />
</cfloop>

<cfoutput>
    #serializeJSON(returnArray)#
</cfoutput>
最近可好 2025-01-14 01:34:31

您的搜索词是

WHERE NAME LIKE '%#trim(ARGUMENTS.search)#%' 

如果我正确理解您的问题的整个单词,它应该是

WHERE NAME LIKE '#trim(ARGUMENTS.search)#' 

注意已删除 % 通配符

但是您可能还会问问题“我的自动完成功能根本不起作用”在这种情况下,我将下载并安装 fiddler 因为这是一个很棒的调试工具,并且非常适合通过 AJAX 调用进行调试。

Your search term is

WHERE NAME LIKE '%#trim(ARGUMENTS.search)#%' 

If I understand your question correctly for whole words it shoud be

WHERE NAME LIKE '#trim(ARGUMENTS.search)#' 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文