jquery.suggest.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * 在jquery.suggest 1.1基础上针对中文输入的特点做了部分修改,下载原版请到jquery插件库
  3. * 修改者:wangshuai
  4. *
  5. * 修改部分已在文中标注
  6. *
  7. *
  8. * jquery.suggest 1.1 - 2007-08-06
  9. *
  10. * Uses code and techniques from following libraries:
  11. * 1. http://www.dyve.net/jquery/?autocomplete
  12. * 2. http://dev.jquery.com/browser/trunk/plugins/interface/iautocompleter.js
  13. *
  14. * All the new stuff written by Peter Vulgaris (www.vulgarisoip.com)
  15. * Feel free to do whatever you want with this file
  16. *
  17. */
  18. (function($) {
  19. $.suggest = function(input, options) {
  20. var $input = $(input).attr("autocomplete", "off");
  21. var $results = $("#sr_infos");
  22. var timeout = false; // hold timeout ID for suggestion results to appear
  23. var prevLength = 0; // last recorded length of $input.val()
  24. $input.blur(function() {
  25. setTimeout(function() { $results.hide() }, 200);
  26. });
  27. $results.mouseover(function() {
  28. $("#sr_infos ul li").removeClass(options.selectClass);
  29. })
  30. // help IE users if possible
  31. try {
  32. $results.bgiframe();
  33. } catch(e) { }
  34. // I really hate browser detection, but I don't see any other way
  35. //修改开始
  36. //下面部分在作者原来代码的基本上针对中文输入的特点做了些修改
  37. if ($.browser.mozilla)
  38. $input.keypress(processKey2); // onkeypress repeats arrow keys in Mozilla/Opera
  39. else
  40. $input.keydown(processKey2); // onkeydown repeats arrow keys in IE/Safari*/
  41. //这里是自己改为keyup事件
  42. $input.keyup(processKey);
  43. //修改结束
  44. function processKey(e) {
  45. // handling up/down/escape requires results to be visible
  46. // handling enter/tab requires that AND a result to be selected
  47. if (/^32$|^9$/.test(e.keyCode) && getCurrentResult()) {
  48. if (e.preventDefault)
  49. e.preventDefault();
  50. if (e.stopPropagation)
  51. e.stopPropagation();
  52. e.cancelBubble = true;
  53. e.returnValue = false;
  54. selectCurrentResult();
  55. } else if ($input.val().length != prevLength) {
  56. if (timeout)
  57. clearTimeout(timeout);
  58. timeout = setTimeout(suggest, options.delay);
  59. prevLength = $input.val().length;
  60. }
  61. }
  62. //此处针对上面介绍的修改增加的函数
  63. function processKey2(e) {
  64. // handling up/down/escape requires results to be visible
  65. // handling enter/tab requires that AND a result to be selected
  66. if (/27$|38$|40$|13$/.test(e.keyCode) && $results.is(':visible')) {
  67. if (e.preventDefault)
  68. e.preventDefault();
  69. if (e.stopPropagation)
  70. e.stopPropagation();
  71. e.cancelBubble = true;
  72. e.returnValue = false;
  73. switch(e.keyCode) {
  74. case 38: // up
  75. prevResult();
  76. break;
  77. case 40: // down
  78. nextResult();
  79. break;
  80. case 27: // escape
  81. $results.hide();
  82. break;
  83. case 13: // enter
  84. $currentResult = getCurrentResult();
  85. if ($currentResult) {
  86. $input.val($currentResult.text());
  87. window.location.href='?m=search&c=index&a=init&q='+$currentResult.text();
  88. }
  89. break;
  90. }
  91. } else if ($input.val().length != prevLength) {
  92. if (timeout)
  93. clearTimeout(timeout);
  94. timeout = setTimeout(suggest, options.delay);
  95. prevLength = $input.val().length;
  96. }
  97. }
  98. function suggest() {
  99. var q = $input.val().replace(/ /g, '');
  100. if (q.length >= options.minchars) {
  101. $.getScript(options.source+'&q='+q, function(){
  102. });
  103. $results.hide();
  104. //var items = parseTxt(txt, q);
  105. //displayItems(items);
  106. $results.show();
  107. } else {
  108. $results.hide();
  109. }
  110. }
  111. function displayItems(items) {
  112. if (!items)
  113. return;
  114. if (!items.length) {
  115. $results.hide();
  116. return;
  117. }
  118. var html = '';
  119. for (var i = 0; i < items.length; i++)
  120. html += '<li>' + items[i] + '</li>';
  121. $results.html(html).show();
  122. $results
  123. .children('li')
  124. .mouseover(function() {
  125. $results.children('li').removeClass(options.selectClass);
  126. $(this).addClass(options.selectClass);
  127. })
  128. .click(function(e) {
  129. e.preventDefault();
  130. e.stopPropagation();
  131. selectCurrentResult();
  132. });
  133. }
  134. function getCurrentResult() {
  135. if (!$results.is(':visible'))
  136. return false;
  137. //var $currentResult = $results.children('li.' + options.selectClass);
  138. var $currentResult = $("#sr_infos ul li."+ options.selectClass);
  139. if (!$currentResult.length)
  140. $currentResult = false;
  141. return $currentResult;
  142. }
  143. function selectCurrentResult() {
  144. $currentResult = getCurrentResult();
  145. if ($currentResult) {
  146. $input.val($currentResult.text());
  147. $results.hide();
  148. if (options.onSelect)
  149. options.onSelect.apply($input[0]);
  150. }
  151. }
  152. function nextResult() {
  153. $currentResult = getCurrentResult();
  154. if ($currentResult) {
  155. $currentResult.removeClass(options.selectClass).next().addClass(options.selectClass);
  156. } else {
  157. $("#sr_infos ul li:first-child'").addClass(options.selectClass);
  158. //$results.children('li:first-child').addClass(options.selectClass);
  159. }
  160. }
  161. function prevResult() {
  162. $currentResult = getCurrentResult();
  163. if ($currentResult)
  164. $currentResult.removeClass(options.selectClass).prev().addClass(options.selectClass);
  165. else
  166. //$results.children('li:last-child').addClass(options.selectClass);
  167. $("#sr_infos ul li:last-child'").addClass(options.selectClass);
  168. }
  169. }
  170. $.fn.suggest = function(source, options) {
  171. if (!source)
  172. return;
  173. options = options || {};
  174. options.source = source;
  175. options.delay = options.delay || 100;
  176. options.resultsClass = options.resultsClass || 'ac_results';
  177. options.selectClass = options.selectClass || 'ac_over';
  178. options.matchClass = options.matchClass || 'ac_match';
  179. options.minchars = options.minchars || 1;
  180. options.delimiter = options.delimiter || '\n';
  181. options.onSelect = options.onSelect || false;
  182. this.each(function() {
  183. new $.suggest(this, options);
  184. });
  185. return this;
  186. };
  187. })(jQuery);