注意,这个是和前一个$("#txt").autocomplete分开写的,写在上面那个绑定方法的最下面就好了。
$("#txt").result(function(event, data, formatted) {
});
$("#txt").autocomplete("/asmx/executeplan.ashx", {
extraparams:{hosid:hosid,profid:profid},
minchars: 0,
max: 700,
mustmatch: true,
autofill: true,
selectfirst: true,
scrollheight: 220,
width:640,
scroll:true,
parse: function(data){
var obj=eval("("+data+")");
var row = [];
for(var i = 0; i<obj.length; i++){
row[i] = {data:obj[i], //json
value: obj[i].instname, //列表中中的值
result: obj[i].instname}; //显示到下拉列表中的
}
if(obj.length == 1)
{
// $("#hfinstid").val(obj[0].executeid);
// $("#button1").click();
}
return row;
} ,
formatitem: function(row, i, total) {
},
formatmatch: function(row, i, max) {
},
formatresult: function(row, i, max) {
}
});
看个详细说明
需要jquery版本:1.2.6
二、使用
<script src="./jquery-1.3.2.js" type="text/网页特效"></script>
<script src="./jquery.autocomplete.js" type="text/javascript"></script>
<link rel="stylesheet" href="./jquery.autocomplete.css教程" />
autocomplete( url or data, options ) returns: jquery
让一个input或textarea有autocomplete功能
例子
js代码
var data = "core selectors attributes traversing manipulation css events effects ajax utilities".split(" ");
$("#example").autocomplete(data);
以上的例子就是为id为example添加autocomplete
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" }
];
//emails的数组格式如上,formatitem代表的是显示的格式,formatmatch表示匹配的内容,formatresult表示结果的内容
$("#suggest13").autocomplete(emails, {
minchars: 0,
width: 310,
matchcontains: true,
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;
}
});
看一个与jquery 的详细实例
<div class="search_item_content">
<b>商品搜索</b>
<input type="text" id="quick_search" name="q" value="" />
<input type="submit" value="搜索" class="btn" />
</div>
js代码
jquery(document).ready(function() {
jquery("#quick_search").autocomplete(
"$!{request.contextpath}/autocomplete.shtml",
{
max: 10,
scroll: false,
width: 162
}
);
})
java代码
public class autocompleteaction extends action {
public actionforward execute(actionmapping mapping, actionform form,
https教程ervletrequest request, httpservletresponse response)
throws exception {
searchsuggestionmanager service = (searchsuggestionmanager) webapputil
.getservice("searchsuggestionservice", request);
string keyword = request.getparameter("q");
keyword=escapeunescape.unescape(keyword);
list<searchsuggestion> results = service
.getsearchsuggestionsbykeyword(keyword);
try {
response.setcontenttype("text/html; charset=gbk");
printwriter out = response.getwriter();
jsonarray jsonarray = jsonarray.fromobject(results);
out.print(jsonarray.tostring());
out.flush();
out.close();
return null;
} catch (exception e) {
e.printstacktrace();
return null;
}}
}