先看看整个的效果图:
图一:[img]http://files.jb51.net/upload/201104/20110411215756108.gif[/img]
图二:[img]http://files.jb51.net/upload/201104/20110411215756821.gif[/img]
图三:[img]http://files.jb51.net/upload/201104/20110411215756989.gif[/img]
图四:[img]http://files.jb51.net/upload/201104/20110411215756867.gif[/img]
大概的效果图就这样,接下来直接看源码
页面:
[url=autoSearchText.css]<script src="jquery-1.5.1.min.js" type="text/javascript"></script>
<%if (false){ %>
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<%} %>
<script src="jquery.autoSearchText.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#autoSearchText').autoSearchText({ width: 300, itemHeight: 150,minChar:1, datafn: getData, fn: alertMsg });
});
function alertMsg(vl){
alert('你将要搜索的关键字是: '+vl);
}
/*加载数据*/
function getData(val) {
var arrData = new Array();
if (val != "") {
$.ajax({
type: "post",
async: false, //控制同步
url: "getData.ashx",
data: "param=" + val,
dataType: "json",
cache: false,
success: function(data) {
for (var i = 0; i < data.length; i++) {
if (val == data[i].Code.substring(0, val.length))
arrData.push(data[i].Code);
}
},
Error: function(err) {
alert(err);
}
});
}
return arrData;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="autoSearchText" type="text" value="请输入编码" enableviewstate="false"/>
<br />
<input id="Text1" type="text" style=" display:none;"/>
</div>
</form>
</body>
</html>
CSS:
.autoSearchText{
border:solid 1px #CFCFCF;
height:20px;
color:Gray;
}
.menu_v{
margin:0;
padding:0;
line-height:20px;
font-size:12px;
list-style-type:none;
}
.menu_v li{
margin:0;
padding:0;
line-height:20px;
font-size:14px;
list-style-type:none;
float:none;
}
.menu_v li span{
color:Red;
}
#autoSearchItem{
border:solid 1px #CFCFCF;
visibility:hidden;
position:absolute;
background-color:white;
overflow-y:auto;
}
JS:
[url=http://xiazai.jb51.net/201104/yuanma/autoSearch.rar]Demo下载[/url]