Javascript教程_关于Jquery的自动完成插件实例 AutoComplete源码教程
ASP静态数据源
<%
str = "[{'JobName':' Group Leader, Product Management CN and APA'},{'JobName':' Manufacturing Engineer-EM, Nanjing, S&C, ACS'},{'JobName':' secured loan head'},{'JobName':'?单元负责人 - 大理'}]"
response.Write str
%>
ASP动态数据源
<%
sql = "select top 4 * from (select distinct(jobname) from jobs where isdeleted=0 and jobname<>'') as a"
dim str
str = "["
set rs = server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
i=1
if rs.recordcount>0 then
do while not rs.eof
if i=1 then
str = str&"{'JobName':'"&replace(rs("jobname"),"'","")&"'}"
else
str = str&",{'JobName':'"&replace(replace(replace(rs("jobname"),"'",""),"""",""),"","")&"'}"
end if
i=i+1
rs.movenext
loop
end if
rs.close
set rs = nothing
conn.close
set conn = nothing
str = str&"]"
'Response.CharSet = "GB2312"
response.Write str
%>
这两段代码的输出结果是一模一样的,可在利用jquery-autocomplete插件调用时,前者很完美,后者无反应!
jquery代码如下:
<script type="text/javascript">
/*==========用户自定义方法==========*/
//职位数据
var jobList;
//autocomplete选项
var options = {
minChars: 1,
max: 500,
width: 250,
scroll: true,
height: 500,
matchContains: true,
formatItem: function(row, rowNum, rowCount, searchItem)
{
return rowNum + "/" + rowCount + ": \"" + row.JobName ;
},
formatMatch: function(row, rowNum, rowCount)
{
return row.JobName;
},
formatResult: function(row, rowNum, rowCount)
{
return row.JobName;
}
};
//autocomplete初始化函数
function initAutoComplete(data)
{
jobList = data;
$("#inputCityName").autocomplete(jobList, options);
}
/*==========加载时执行的语句==========*/
$(function()
{
//$.getJSON(url, null, initAutoComplete)
$.ajax({
url:"test4.asp?d="+Math.random(),
dataType:'json',
success:initAutoComplete
});
});
</script>