日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery+css模拟下拉框模糊搜索的实现

發布時間:2025/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery+css模拟下拉框模糊搜索的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

html:

@*輸入框*@ <div><input type="text" style="width: 85%; height: 34px;" onkeyup="InputChange(this)" id="txtInput"> </div> @*模擬下拉框*@ <div class="divselect" id="dpSelect"></div>

css:

/*選擇框效果*/ .divselect {float: left;position: relative;z-index: 3003;background: #fff;display: none;width: 85%; } .divselect ul {padding: 0;margin: 0;border: 1px solid #E4E4E4;background-color: #ffffff;position: absolute;z-index: 30000;margin-top: -1px;width: 100%;overflow: auto;max-height: 200px; } .divselect ul li {list-style-type: none;cursor: pointer;height: 24px;line-height: 24px; } .divselect ul li:hover {background: #ccc; }

js:

<script type="text/javascript">//點擊模擬下拉框以外的地方 下拉框消失$(document).bind('click', function (e) {var e = e || window.event; //瀏覽器兼容性var elem = e.target || e.srcElement;while (elem) { //循環判斷至跟節點,防止點擊的是div子元素if (elem.id && elem.id == 'dpSelect') {return;}elem = elem.parentNode;}$('#dpSelect').css('display', 'none'); //點擊的不是div或其子元素 });//用文本框onkeyup事件觸發InputChange方法 InputChange方法判斷文本框文字是否改變 文字改變則觸發SearchName方法var sOldValue;sOldValue = "";function InputChange(arg) {var vNewValue = $(arg).val();if (sOldValue != vNewValue) {//根據條件查詢結果并給下拉框動態賦值 SearchName(arg);sOldValue = vNewValue;}}function SearchName(arg) {var name = $(arg).val();//如果搜索框為空 則返回falseif (name == "") {$("#dpSelect").attr("style", "display:none");return false;}else {$("#dpSelect").attr("style", "display:block");}//獲取數據 并給下拉框動態賦html $.ajax({type: "post",url: "......",data: { name: name },dataType: "",async: false,success: function (data) {var strs = "";strs += "<ul>";for (var i = 0; i < data.length; i++) {strs += '<li οnclick="SetValue(this)">' + data[i] + '</li>';}strs += "</ul>";$("#dpSelect").html(strs);},error: function () {alert("查詢出錯");}});}//點擊模擬下拉框內選項后 給文本框賦值 關閉下拉框function SetValue(arg) {var value = $(arg).text();$("#txtInput").val(value);$("#dpSelect").attr("style", "display:none");} </script>

?

轉載于:https://www.cnblogs.com/blazeZzz/p/7927891.html

總結

以上是生活随笔為你收集整理的jQuery+css模拟下拉框模糊搜索的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。