输入过滤筛选下拉信息(类似百度输入)
生活随笔
收集整理的這篇文章主要介紹了
输入过滤筛选下拉信息(类似百度输入)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引入 jquery
css
<style type="text/css">.search{ left: 0; display: inline;position: relative; } #auto_div{ display: none; width: 196px; border: 1px #74c0f9 solid; background: #FFF; position: absolute; top: 25px; left: 0; color: #323232; z-index:10;overflow-y:scroll;height:190px;}
</style>
script<script type="text/javascript">//var test_list = []; $.ajax({url: '后臺訪問',type: "post",//dataType: "json",success: function (source) {test_list = source;//直接返回list集合即可,本例 test_list.rows( rows 對象)}});var old_value = ""; var highlightindex = -1; //高亮 //自動完成 function AutoComplete(auto, search, mylist) { if ($("#" + search).val() != old_value || old_value == "") { var autoNode = $("#" + auto); //緩存對象(彈出框) var carlist = new Array(); var n = 0; old_value = $("#" + search).val(); for (i in mylist) { if (mylist[i].name.indexOf(old_value) >= 0) { carlist[n++] = mylist[i].name; } } if (carlist.length == 0) { autoNode.hide(); return; } autoNode.empty(); //清空上次的記錄 for (i in carlist) { var wordNode = carlist[i]; //彈出框里的每一條內容 var newDivNode = $("<div>").attr("id", i); //設置每個節點的id值 newDivNode.attr("style", "font:14px/25px arial;height:25px;padding:0 8px;cursor: pointer;"); newDivNode.html(wordNode).appendTo(autoNode); //追加到彈出框 //鼠標移入高亮,移開不高亮 newDivNode.mouseover(function () { if (highlightindex != -1) { //原來高亮的節點要取消高亮(是-1就不需要了) autoNode.children("div").eq(highlightindex).css("background-color", "white"); } //記錄新的高亮節點索引 highlightindex = $(this).attr("id"); $(this).css("background-color", "#ebebeb"); }); newDivNode.mouseout(function () { $(this).css("background-color", "white"); }); //鼠標點擊文字上屏 newDivNode.click(function () { //取出高亮節點的文本內容 var comText = autoNode.hide().children("div").eq(highlightindex).text(); highlightindex = -1; //文本框中的內容變成高亮節點的內容 $("#" + search).val(comText); }) if (carlist.length > 0) { //如果返回值有內容就顯示出來 autoNode.show(); } else { //服務器端無內容返回 那么隱藏彈出框 autoNode.hide(); //彈出框隱藏的同時,高亮節點索引值也變成-1 highlightindex = -1; } } } //點擊頁面隱藏自動補全提示框 document.onclick = function (e) { var e = e ? e : window.event; var tar = e.srcElement || e.target; if (tar.id != search) { if ($("#" + auto).is(":visible")) { $("#" + auto).css("display", "none") } } } } $(function () { old_value = $("#gdsNameId").val(); $("#gdsNameId").focus(function () { if ($("#gdsNameId").val() == "") { AutoComplete("auto_div", "gdsNameId", test_list.rows); } }); $("#gdsNameId").keyup(function () { AutoComplete("auto_div", "gdsNameId", test_list.rows); }); }); </script>
html
<div class="search"> <input id="gdsNameId" type="text" autocomplete="off" name="gdsName" value="${gdsName}" class="form-control"/><div id="auto_div"></div> </div>總結
以上是生活随笔為你收集整理的输入过滤筛选下拉信息(类似百度输入)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【NLP】面向对话的机器阅读理解任务(D
- 下一篇: 【Python】相当全面的Pandas文