jquery autocomplete 使用

阎祖鹤
2023-12-01

本地格式:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>自定义提示</title>
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.autocomplete.min.js"></script>
<link rel="Stylesheet" href="/js/jquery.autocomplete.css" />
<script type="text/javascript">
var emails = [
{ name: "Peter Pan", to: "peter@pan.de" },
{ name: "Molly", to: "molly@yahoo.com" },
{ name: "Forneria Marconi", to: "live@japan.jp" },
{ name: "Master <em>Sync</em>", to: "205bw@samsung.com" },
{ name: "Dr. <strong>Tech</strong> de Log", to: "g15@logitech.com" },
{ name: "Don Corleone", to: "don@vegas.com" },
{ name: "Mc Chick", to: "info@donalds.org" },
{ name: "Donnie Darko", to: "dd@timeshift.info" },
{ name: "Quake The Net", to: "webmaster@quakenet.org" },
{ name: "Dr. Write", to: "write@writable.com" },
{ name: "GG Bond", to: "Bond@qq.com" },
{ name: "Zhuzhu Xia", to: "zhuzhu@qq.com" }
];
$(function() {
$('#keyword').autocomplete(emails, {
max: 12, //列表里的条目数
minChars: 0, //自动完成激活之前填入的最小字符
width: 400, //提示的宽度,溢出隐藏
scrollHeight: 300, //提示的高度,溢出显示滚动条
matchContains: true, //包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示
autoFill: false, //自动填充
formatItem: function(row, i, max) {
return i + '/' + max + ':"' + row.name + '"[' + row.to + ']';
},
formatMatch: function(row, i, max) {
return row.name + row.to;
},
formatResult: function(row) {
return row.to;
}
}).result(function(event, row, formatted) {
alert(row.to);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="keyword" />
<input id="getValue" value="GetValue" type="button" />
</div>
</form>
</body>
</html>
servlet格式

$("#keyword'").autocomplete(../servlet {
  max: 12, //列表里的条目数
  minChars: 0, //自动完成激活之前填入的最小字符
  width: 400, //提示的宽度,溢出隐藏
  scrollHeight: 300, //提示的高度,溢出显示滚动条
  matchContains: true, //包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示
  autoFill: false, //自动填充 
  extraParams: {   
            sname: function(){return $("#sName").val();},
    idcard: function(){return $("#idCard").val();}
        },
           parse : function(data) {  
            return $.map(eval(data), function(row) {  
                        return {  
                            data : row,  
                            value : row.name,  
                            result : row.name  
                        }  
                    });  
        },  

        formatItem: function(row, i, max) {     
            return "发件人:"+row.name+", 收件人:"+row.to;   //下拉列表格式   
          },     
          formatMatch: function(row, i, max) {     
            return row.name+row.to;      //与这些相匹配
          },     
          formatResult: function(row) {     
            return row.name;     //最终得到的结果
          }     
 }).result(function(event,row,formatted){
  $("#idCard").val(row.card);//通过result函数可进对数据进行其他操作
 });

 

 

String s=req.getParameter("sname");
    String c=req.getParameter("idcard");
    String sname=new String(s.getBytes("ISO-8859-1"),"UTF-8");
    String idcard=new String(c.getBytes("ISO-8859-1"),"UTF-8");
    StuffService ss= (StuffService) applicationContext.getBean("stuffService");
    list= ss.queryIdcardByName(sname,idcard);
    Iterator it=list.iterator();
    StringBuffer stringBuffer = new StringBuffer();  
          stringBuffer.append("[");  
    while(it.hasNext()){
      Stuff stuff=(Stuff)it.next();
      stringBuffer.append("{ name: '" + stuff.getsName() + "', card: '" + stuff.getIdCard() + "' }, ");  
    }
     stringBuffer.replace(stringBuffer.length()-3, stringBuffer.length(), "}");
     stringBuffer.append("]"); 
     System.out.println(stringBuffer);  
           out.print(stringBuffer);  
           out.flush();  
           out.close();

通过servlet动态生成的stringbuffer输出语句格式 和 本地格式的email相同;

 

 

 类似资料: