首页 > 其他分享 >下拉可进行模糊查询

下拉可进行模糊查询

时间:2024-04-13 14:55:34浏览次数:23  
标签:style 模糊 value 查询 selText 下拉可 var document tmpObj

效果图:

1、无默认值

 2、可进行输入模糊查询

 

3、不进行查询可直接点击右侧部分整个下拉出现

 


<div class="form-in"> <label class="control-label long"> 地区名称: </label> <div class="long b" style="position: relative;"> <%-- 引用下拉模糊查询(具体使用时可以调整width、height、top的值,适应自己的项目) --%> <%-- 输入框(页面显示):输入、下拉模糊查询--%> <asp:TextBox ID="txtDropDownInput" runat="server" onfocus="this.select();InputValue(this)" Style="z-index: 1; position: absolute; top: 2px; width: 200px; height: 30px;" onkeyup="InputValue(this)" onblur="NoOnblur()"></asp:TextBox> <%-- 原下拉组件(页面隐藏):后台绑定数据源 --%> <asp:DropDownList ID="ddl_Bank_Name" runat="server" Style="position: relative; top: 0; height: 19px; width: 260px;" onclick="SelectValue(this)" onchange="SelectValue(this)" OnSelectedIndexChanged="bankChange" AutoPostBack="true"></asp:DropDownList> <div id="selectLikeDiv" style="border: 1px solid #FFFFFF; white-space: nowrap; overflow: hidden; position: absolute; display: block; top: 25px;"></div> <%-- 存储下拉选择的值 --%> <input type="hidden" name="txtSection" id="txtSection" /> </div> </div>
      /**********************************************/
   //DropDownList下拉选择实现模糊查询的JS

   var txtDropDownInputId = "<%=txtDropDownInput.ClientID %>";//使用时修改 
   var dropDownListAspId = "<%=ddl_Bank_Name.ClientID %>";//使用时修改 
    
   var j = 0;
   function SelectValue(obj) {
       var input = obj.parentNode.nextSibling;
       document.getElementById(txtDropDownInputId).value = obj.options[obj.selectedIndex].text;
       document.getElementById("txtSection").value = obj.options[obj.selectedIndex].value;
   }
   //输入触发事件
   function InputValue(obj) {
       var n = 1;
       var tmpObj;
       var src = document.getElementById(dropDownListAspId);
       var selectLikeDiv = document.all.selectLikeDiv;
       if (event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13) {
           if (obj.value != "") {
               selectLikeDiv.style.display = "";
               selectLikeDiv.innerHTML = "";
               if (selectLikeDiv.hasChildNodes()) {
                   selectLikeDiv.childNodes[0].parentNode.removeChild(selectLikeDiv.childNodes[0]);
               }

               for (var i = 0; i < src.length; i++) {
                   var selValue = document.createElement("div");
                   var selText = document.createElement("div");
                   selText.value = src[i].value;
                   selText.innerHTML = src[i].text;
                   selText.style.backgroundColor = '#ffffff';
                   selText.style.color = '#000000';

                   if (src[i].text.toLowerCase().indexOf(obj.value.toLowerCase()) > -1) {
                       selText.setAttribute("id", "selText" + n);
                       selText.onmouseover = function () {
                           this.style.backgroundColor = '#003399';
                           this.style.color = '#ffffff';
                       }
                       selText.onmouseout = function () {
                           this.style.backgroundColor = '#ffffff';
                           this.style.color = '#000000';
                       }
                       selText.onclick = function () {
                           document.getElementById(txtDropDownInputId).value = this.innerHTML;
                           selectLikeDiv.style.display = "none";
                           document.getElementById("txtSection").value = this.value;
                           set_select_checked(dropDownListAspId, this.value);

                           setTimeout('__doPostBack(\'ctl00$MainContent$cboCashBank\',\'\')', 0);//如果DropDownList 使用了AutoPostBack="true",就要使用这块(可以在页面中F12查看到代码,粘贴过来即可)
                       }
                       selectLikeDiv.appendChild(selText);
                       n++;
                   }
                  
               }
           }
           else {
               document.all.selectLikeDiv.style.display = "none";
           }
       }
       else {
           //press down key
           if (event.keyCode == 40) {
               j++;
               for (var i = 0; i < src.length; i++) {
                   tmpObj = document.getElementById("selText" + i);
                   if (tmpObj != null) {
                       tmpObj.style.backgroundColor = '#ffffff';
                       tmpObj.style.color = '#000000';
                   }
               }
               tmpObj = document.getElementById("selText" + j);
               if (tmpObj != null) {
                   tmpObj.style.backgroundColor = '#003399';
                   tmpObj.style.color = '#ffffff';
               } else {
                   j = 0;
               }
           }
           //press up key
           if (event.keyCode == 38) {
               j--;
               for (var i = 0; i < src.length; i++) {
                   tmpObj = document.getElementById("selText" + i);
                   if (tmpObj != null) {
                       tmpObj.style.backgroundColor = '#ffffff';
                       tmpObj.style.color = '#000000';
                   }
               }
               tmpObj = document.getElementById("selText" + j);
               if (tmpObj != null) {
                   tmpObj.style.backgroundColor = '#003399';
                   tmpObj.style.color = '#ffffff';
               } else {
                   j = 2;
               }
           }
           //press enter key
           if (event.keyCode == 13) {
               tmpObj = document.getElementById("selText" + j);
               document.getElementById(txtDropDownInputId).value = tmpObj.innerHTML;
               selectLikeDiv.style.display = "none";
               document.getElementById("txtSection").value = tmpObj.value;
           }
       }
   }

   function SelMatch(src) {
       var currSel = document.getElementById(txtDropDownInputId).value;
       for (var i = 0; i < src.length; i++) {
           if (src[i].text == currSel) {
               src.options[i].selected = true;
           }
       }
   }
   
   //失去焦点执行事件
   function NoOnblur() {
       //加延时处理,才能实现
       setTimeout(function () { OnblurSelectLikeDiv(); }, 100);
   }

   function OnblurSelectLikeDiv() {
       if (document.activeElement.id == "selectLikeDiv") {
           return false;
       } else {
           document.all.selectLikeDiv.style.display = 'none';
       }
   }



   function set_select_checked(selectId, checkValue) {
       var select = document.getElementById(selectId);

       for (var i = 0; i < select.options.length; i++) {
           if (select.options[i].value == checkValue) {
               select.options[i].selected = true;
               break;
           }
       }
   } 

   /**********************************************/
  var dt =数据库数据;
         
      foreach (DataRow dr in dt.Rows)
      {
          dropDownList.Items.Add(new ListItem(dr["name"].ToString(), dr["code"].ToString()));
      } 

 

标签:style,模糊,value,查询,selText,下拉可,var,document,tmpObj
From: https://www.cnblogs.com/snrj/p/18132858

相关文章

  • Sql 分页查询
    SELECT*FROM(SELECT*,ROW_NUMBER()OVER(ORDERBY会员编号DESC)ASRowNumberFROMMold_List)TWHERET.RowNumberBETWEEN@PageSize*(@StartPage-1)+1AND@PageSize*@StartPage辅助类:namespaceMvcMovie.Utility{///<summary>///分页帮助类......
  • [转帖]SQL Server简洁查询正在运行的进程
    SQLServer简洁查询正在运行的进程通常我们可以使用sp_who2我们希望更加简洁的信息,下面这个查询使用系统表sys.sysprocesses,以及sys.dm_exec_sql_text做OUTERAPPLY.T-SQL是这样的:SELECTspid,blocked,DB_NAME(sp.dbid)ASDBName,program_name,waitresource,last......
  • 使用内置函数 (SQL Server) 验证、查询和更改 JSON 数据
    使用内置函数(SQLServer)验证、查询和更改JSON数据项目2023/09/0313个参与者反馈 本文内容此页上的示例JSON文本使用ISJSON函数验证JSON文本使用JSON_VALUE函数从JSON文本中提取值使用JSON_QUERY函数从JSON文本中提取对象或数组显示另外......
  • mysql-子查询的学习
    子查询由一个具体的需求,引入子查询谁的工资比Abel的高SELECT*fromemployeesWHEREsalary>(SELECTsalaryFROMemployeesWHERElast_name='Abel')--自连接SELECTe2.*......
  • lightdb 支持从父查询中返回子查询表中的 rowid
    背景在如下的sql中,selectrowidfrom(select1fromt);lightdb24.1以前会直接报错,说找不到rowid列。为了兼容Oracle,在24.1中,我们选择将告知子查询除了返回子查询应有的列之外,还需返回rowid列。样例以下样例sql说明了lightdb24.1中支持的rowid的功能:-......
  • 子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作
    行重复   删除掉一行即可deletefromrolewhereid=9goSETIDENTITY_INSERT[dbo].[role]ONGOINSERTINTO[dbo].[role]([id],[description],[role_key],[role_value])VALUES(N'9',N'这是超级管理员',N'ROLE_ADMIN',N'超级管理员')......
  • Elastic Search term, match, match_phrase 和 match_phrase_prefix 查询时的区别
    字段类型分keyword(不分词)和text(分词)字段值为"quickbrownfox",详细的createindex和insertdata语句在后边注意:keyword类型字段不分词,text类型字段keyword类型的字段值就是"quickbrownfox",不分词,必须当成一个整体text类型的字段值是"quickbrownfox",分词,可以当成......
  • Avalonia下拉可搜索树(TreeComboBox)
    1.需求分析  树形下拉的功能是ComboBox和TreeView的功能结合起来,再结合数据模板来实现这一功能。2.代码实现 1.创建UserControl集成TreeView控件`publicclassTreeComboBox:TreeView{privatebool_isPushTextChangedEvent=true;privateButtonClearButton;pri......
  • 腾讯云服务器月租费用查询及2024年优惠购买入口全掌握
    在探索云服务市场时,许多用户都对腾讯云服务器的价格颇为关注。那么,腾讯云服务器一个月究竟需要多少钱呢?其实,这取决于您选择的服务器配置和购买方案。在腾讯云【爆品专区】,有多种服务器配置可选,价格亲民。例如,腾讯云轻量2核2G3M服务器,一年仅需61元,折合一个月仅5.08元。如果您需......
  • 为何申请贷款时需要查询大数据信用查询?又该如何查询自己的大数据信用报告呢?
    在这个数字化飞速发展的时代,大数据技术在金融行业的应用尤为显著。很多人可能会疑惑,为何申请贷款时总是需要进行大数据信用查询?又该如何查询自己的大数据信用报告呢?本文将为您详细解答这些问题。首先,我们来探讨一下贷款过程中大数据信用查询的几个重要原因:1.......