var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; $("#bus").autocomplete( availableTags,{ scroll:false,autoFill:false,matchContains: true,minChars:0} ).result(function(event, data, formatted){ //alert(availableTags); 返回的结果 });
<input id="bus" name="bus" type="text" style="width: 225px" />
动态添加:
$(function(){ var availableTags = []; $.ajax({ type: 'POST', url: $.test.contextPath + "/xxxAction.do?method=xxx", dataType: 'json', success: function(data) { $(data).each(function(){ alert(this.text); availableTags.push(this.text); }); }, error: function(XMLHttpRequest, textStatus, errorThrown) { } }); /* availableTags = [ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ];*/ $("#bus").autocomplete( availableTags,{ scroll:false,autoFill:false,matchContains: true,minChars:0} ).result(function(event, data, formatted){ //alert(availableTags); 返回的结果 }); });
public ActionForward xxx(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject json = new JSONObject(); JSONObject json2 = new JSONObject(); JSONObject json3 = new JSONObject(); JSONArray jsonArray = new JSONArray(); json.put("text","abc"); json2.put("text","ade"); json3.put("text","aef"); jsonArray.add(json); jsonArray.add(json2); jsonArray.add(json3); Utils.writeJsonText(jsonArray.toString(), response); return null; } 返回格式:[{"text":"abc"},{"text":"ade"},{"text":"aef"}]