源码:
//@name Textbox search autocompalety//@version 0.1//@author Ni//@description 文本框搜索插件,支持键盘上下键(function ($) {$.setOptions = function (k, p) {p = $.extend({IsShowSearchList: false, //是否显示搜索列表SearchValue: "", //作为搜索条件的值TextSearchSelectedIndex: "", //当前下拉列表所选的索引TextSearchList: new Array(), //下拉列表的数据源TimeoutSearch: null, //延迟搜索Left: 0, //div显示位置 XTop: 0, //div显示位置 YRecordCount: 0, //符合搜索条件的记录行数TimeoutNum: 500, //【可配置】延迟搜索 单位:毫秒Width: "500px", //【可配置】div宽度Url: "", //【可配置】取值方法地址MinSearchLength: 1, //【可配置】最小搜索关键词长度MaxRowCount: 10, //【可配置】搜索框最多显示行数 显示全部为-1onSelect: false //【可配置】行选中事件}, p);var t = {hideTxtSearchList: function () {clearTimeout(p.TimeoutSearch);if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v')!=null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex != "") {t.selectTxtSearch(p.TextSearchSelectedIndex);}p.IsShowSearchList = false;p.TextSearchSelectedIndex = "";$("#divTxtSearch").hide();$("#divTxtSearch").empty();$("#iframeOpenTxt").hide();},getTxtSearchList: function () {var e = document.getElementById($(k).attr('id'));if (e.value.length < p.MinSearchLength) return;if (event.keyCode == 38 || event.keyCode == 40) {if (p.IsShowSearchList == false) return;//上下移动事件if (event.keyCode == 38)t.selectTxtSearchUp();if (event.keyCode == 40)t.selectTxtSearchDown();p.SearchValue = e.value;return;}p.Left = t.aspnm_pageX(e) + 1;p.Top = t.aspnm_pageY(e) + 20;p.SearchValue = e.value;clearTimeout(p.TimeoutSearch);p.TimeoutSearch = setTimeout(function () {$.ajax({url: p.Url + encodeURI(p.SearchValue) + "&pagesize=" + p.MaxRowCount,dataType: "json",type: "POST",success: function (da) {p.TextSearchList = new Array();p.RecordCount = da.total;for (var i = 0; i < da.rows.length; i++) {p.TextSearchList[i] = da.rows[i].row;}t.crateTxtSearchList();}});}, p.TimeoutNum);},//创建下拉列表crateTxtSearchList: function () {p.IsShowSearchList = true;p.TextSearchSelectedIndex = "";$("#divTxtSearch").empty();var html;if (p.TextSearchList.length > 0) {html = "
" + listItem[j] + " | ";}html += "
未列出所有搜索结果!
";}} else {html = " 搜索完毕,没有结果可以显示!
";}$("#divTxtSearch").show();$("#divTxtSearch").html(html);$("#iframeOpenTxt").show();$("#divTxtSearch").css("left", p.Left);$("#divTxtSearch").css("top", p.Top);$("#divTxtSearch").css("width", p.Width);t.setOpenTxtIframe();},//设置IframesetOpenTxtIframe: function () {$("#iframeOpenTxt").show();$("#iframeOpenTxt").css("left", p.Left);$("#iframeOpenTxt").css("top", p.Top);$("#iframeOpenTxt").css("width", document.getElementById('divTxtSearch').offsetWidth + "px");$("#iframeOpenTxt").css("height", document.getElementById('divTxtSearch').offsetHeight + "px");},//Xaspnm_pageX: function (element) {var x = 0;dox += element.offsetLeft;while ((element = element.offsetParent));return x;},//Yaspnm_pageY: function (element) {var y = 0;doy += element.offsetTop;while ((element = element.offsetParent));return y;},//鼠标的选择行事件selectTxtSearchMouse: function (rowIndex) {if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v') != null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex != '')$("#TRTxtSearch" + p.TextSearchSelectedIndex).attr("class", "TxtSearchNotFoucs");$("#TRTxtSearch" + rowIndex).attr("class", "TxtSearchFoucs");p.TextSearchSelectedIndex = rowIndex;},//点击'上'触发的js事件selectTxtSearchUp: function () {if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v') != null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex == '' || parseInt(p.TextSearchSelectedIndex) == 1) {//最后一个t.selectTxtSearch(p.TextSearchList.length);}else {//上移t.selectTxtSearch(parseInt(p.TextSearchSelectedIndex) - 1);}},//点击'下'触发的js事件selectTxtSearchDown: function () {if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v') != null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex == '' || parseInt(p.TextSearchSelectedIndex) == p.TextSearchList.length) {//第一个t.selectTxtSearch('1');}else {//下移t.selectTxtSearch(parseInt(p.TextSearchSelectedIndex) + 1);}},//选择行selectTxtSearch: function (rowIndex) {if (p.TextSearchSelectedIndex != '')$("#TRTxtSearch" + p.TextSearchSelectedIndex).attr("class", "TxtSearchNotFoucs");$("#TRTxtSearch" + rowIndex).attr("class", "TxtSearchFoucs");p.TextSearchSelectedIndex = rowIndex;//将值写到页面指定元素 方法自己实现var rowData = p.TextSearchList[rowIndex - 1].split(',');if (p.onSelect) p.onSelect(rowData);}};$(k).keyup(function () {t.getTxtSearchList();});$(k).blur(function () {t.hideTxtSearchList();});};//程序入口$.fn.TxtSearch = function (p) {this.each(function () {$.setOptions(this, p);});};})(jQuery);document.write(' '+ ''); 效果图:
调用方式:
//自动搜索 $("#txt1").TxtSearch({ Url: "/httpHandler/SystemManagement/HandlerDemo.ashx?type=getpname&key=", Width: "460", onSelect: function (rowData) { $('#hid1').val(rowData[0]); $('#txt1').val(rowData[1]); } });
private MenuFun _instance = DataFactory.CreateInstance("MenuFun", AssemblyKey.BusRoles); private CommonFun commonFun = DataFactory.CreateInstance ("CommonFun", AssemblyKey.BusComm); public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write(Handler(context)); }public string Handler(HttpContext context) { string src = context.Request["type"]; switch (src) { case "getpname": return GetPName(context); default: return string.Empty; } }string GetPName(HttpContext context) { string pageSize = context.Request["pagesize"]; if (string.IsNullOrEmpty(pageSize)) pageSize = "100"; if (pageSize == "-1") pageSize = "10000"; string where = ""; string key = context.Request["key"]; if (!string.IsNullOrEmpty(key)) { if (Utils.IsNumber(key)) where = string.Format(" and FUNMODEL_CD = '{0}'", key); else if(Utils.IsEnglish(key)) { where = string.Format(" and dbo.f_GetPy(FUNMODEL_NM) like '%{0}%'", key); } else { where = string.Format(" and FUNMODEL_NM like '%{0}%'", key); } } DataSet ds = commonFun.GetLimitPageData("v_CYP_MenuName", "FUNMODEL_CD, FUNMODEL_NM,FUNMODEL_LEV,MAXID", where, "FUNMODEL_CD", 1, int.Parse(pageSize)); if (ds != null && ds.Tables.Count == 2 && ds.Tables[1].Rows.Count > 0) { string json = "{\"total\":" + ds.Tables[0].Rows[0][0] + ",\"rows\":["; foreach (DataRow dataRow in ds.Tables[1].Rows) { json += "{\"row\":\""; json = ds.Tables[1].Columns.Cast ().Aggregate(json, (current, dataColumn) => current + (dataRow[dataColumn] + ",")); json = json.Substring(0, json.Length - 1) + "\"},"; } json = json.TrimEnd(',') + "]}"; return json; } return "{\"total\":0,\"rows\":[]}"; }