jquery autocomplete 为什么我得到的不是数组而是字符串
jquery autocomplete 为什么我得到的不是数组而是字符串?
HTML code<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><html><head> <script type="text/javascript" src="plugins/jquery/js/jquery.js"></script> <script type='text/javascript' src='plugins/jquery/js/jquery.autocomplete.js'></script> <link rel="stylesheet" type="text/css" href="plugins/jquery/css/jquery.autocomplete.css" /> <script type="text/javascript"> $(document).ready(function() { $("#labTime").html("time:" + new Date()); setInterval('$("#labTime").html("time:" + new Date());', 1000); //var cities = ["中文","中国","国家","中文国家"]; //$("#content").focus().autocomplete(cities,{matchContains: true,minChars: 1}); initialComplete(null); }); function initialComplete(data){ $("#content").focus().autocomplete("test!getList", { delay:10, minChars:1, matchSubset:0, matchContains:true, cacheLength:10, autoFill:true }); } </script></head><body> <center> <div> <h4 id="labTime"></h1> <form action="test!execute" method="post"> <img src="images/psb.jpg" width="500px" height="200px"></img><br> <input type="text" name="content" size="50" id="content" /> <input type="submit" value="Search"> </form> </div> </center></body></html>
下面是action的代码
Java code public String getList() throws Exception { System.out.println("content:(" + content + ") q:(" + q + ")"); //String[] str = {"中文","中国","国家","中文国家"}; String[] str = {"a文","a国","家a","b文国家"}; HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=utf-8"); PrintWriter out; out = response.getWriter(); List list = new ArrayList(); for(int i = 0 ; i < str.length; i ++){ list.add(str[i]); } JSONArray array = JSONArray.fromObject( list ); out.println(array.toString()); out.close(); return null; }
我的输入框提示一行["a文","a国","家a","b文国家"],而不是4行数据
------解决方案--------------------
你输入框只有一行,怎么来4行呢?
而且http传输都是以字符串传输的,没有对象和数据的概念。
文本框里的内容也全是字符,没有数据的概念。
------解决方案--------------------
下拉框是select+option,不是input。
再说如果没用框架,它也没这么智能,自动把你的数据组装成select
------解决方案--------------------
需要jQuery 指定下返回数据的格式,并且 你返回的是json 字符串 如果前台不指定的话 需要用json.js 转换为json 对象 就可以用 a.b的形式取值 json.js 可以在网上随便下一个
------解决方案--------------------
查询谷 - www.chaxungu.com
jquery autocomplete 为什么小弟我得到的不是数组而是字符串
编辑:chaxungu时间:2022-10-02 23:14:43分类:web开发