testSearch.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <title></title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. -webkit-box-sizing: border-box;
  13. }
  14. .input-search {
  15. position: relative;
  16. margin: 100px auto;
  17. width: 200px;
  18. }
  19. input {
  20. width: 200px;
  21. height: 30px;
  22. padding-left: 10px;
  23. padding-right: 10px;
  24. background-color: #ffffff;
  25. border: solid 1px #e5e5e5;
  26. text-overflow: ellipsis;
  27. overflow: hidden;
  28. white-space: nowrap;
  29. outline: none;
  30. -webkit-appearance: none;
  31. appearance: none;
  32. }
  33. ul {
  34. display: none;
  35. position: absolute;
  36. top: 29px;
  37. width: 100%;
  38. border: solid 1px #62a6da;
  39. max-height: 340px;
  40. overflow-y: auto;
  41. z-index: 1;
  42. font-size: 0;
  43. }
  44. ul li {
  45. position: relative;
  46. padding-left: 10px;
  47. width: 100%;
  48. height: 34px;
  49. line-height: 34px;
  50. font-size: 14px;
  51. color: #454545;
  52. text-align: left;
  53. background-color: #fff;
  54. cursor: pointer;
  55. overflow: hidden;
  56. text-overflow: ellipsis;
  57. white-space: nowrap;
  58. }
  59. ul li:hover {
  60. color: #fff;
  61. background-color: #1c9dff;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div class="input-search">
  67. <input type="text" placeholder="请输入课程名称">
  68. <ul></ul>
  69. </div>
  70. <script type="text/javascript" src="//code.jquery.com/jquery-1.8.2.min.js"></script>
  71. <script>
  72. $(function () {
  73. /**按下键盘时搜索*/
  74. $("body").undelegate("input", "keyup").delegate("input", "keyup", function () {
  75. var name = $(this).val().replace(/\s+/g, "");
  76. if (name.length > 0) {
  77. var resultArray = getArrayByName(name, jsonArr, 6);
  78. if (resultArray.length < 1) {
  79. $(this).next("ul").hide();
  80. } else {
  81. var html = "";
  82. for (var i = 0; i < resultArray.length; i++) {
  83. html += "<li>" + resultArray[i].name + "</li>";
  84. }
  85. $(this).next("ul").html(html).show();
  86. }
  87. }
  88. })
  89. /**下拉列表展开时点击li的事件,为输入框赋值*/
  90. $(".input-search").on("click", "ul li", function () {
  91. $(this).parent("ul").siblings("input").val($(this).text());
  92. $(this).parent("ul").hide();
  93. });
  94. })
  95. var jsonArr = [
  96. {
  97. "name": "计算机组成原理"
  98. },
  99. {
  100. "name": "计算机网络"
  101. },
  102. {
  103. "name": "数据结构"
  104. },
  105. {
  106. "name": "网页程序设计"
  107. },
  108. {
  109. "name": "嵌入式开发"
  110. },
  111. {
  112. "name": "高等数学"
  113. },
  114. {
  115. "name": "计算机应用技术"
  116. },
  117. {
  118. "name": "计算机科学与技术"
  119. }
  120. ]
  121. /**
  122. * 根据字符串模糊搜索返回符合条件的数据
  123. * name 搜索字符串
  124. * array 检索json数组
  125. * length 匹配结果最大长度
  126. */
  127. function getArrayByName(name, array, length) {
  128. if (array.length < 1) {
  129. return;
  130. }
  131. var result = [];
  132. for (var key in array) {
  133. if (checkStrContain(array[key].name, name) && result.length < length) {
  134. result.push(array[key])
  135. }
  136. }
  137. return result
  138. }
  139. /**检查一个字符串是否包含在另一个字符串里,并且首字符相同
  140. * i:计算机科学与技术
  141. * j:计科
  142. * 返回true
  143. * */
  144. function checkStrContain(i, j) {
  145. if (!i || !j) {
  146. return false;
  147. }
  148. if (i.charAt(0) != j.charAt(0)) {
  149. return false;
  150. }
  151. i = i.substr(1, i.length - 1);
  152. j = j.substr(1, j.length - 1);
  153. var a;
  154. var b;
  155. if (i.length > j.length) {
  156. a = i;
  157. b = j;
  158. } else {
  159. a = j;
  160. b = i;
  161. }
  162. var count = 0;
  163. for (var bi = 0; bi < b.length; bi++) {
  164. var bArr = b.split("");
  165. if (a.indexOf(bArr[bi]) != -1) {
  166. count++;
  167. } else {
  168. break;
  169. }
  170. }
  171. if (b.length == count) {
  172. return true;
  173. } else {
  174. return false;
  175. }
  176. }
  177. </script>
  178. </body>
  179. </html>