jqautocompletewap.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. !function ($) {
  2. $.fn.serializeObject = function() {
  3. var o = {};
  4. var a = this.serializeArray();
  5. $.each(a, function() {
  6. if (o[this.name]) {
  7. if (!o[this.name].push) {
  8. o[this.name] = [o[this.name]];
  9. }
  10. o[this.name].push(this.value || '');
  11. } else {
  12. o[this.name] = this.value || '';
  13. }
  14. });
  15. return o;
  16. };
  17. //autocomplete
  18. var Autocomplete = function (input, o) {
  19. this.o = $.extend(true, {}, Autocomplete.def, o);
  20. this.$sWord = $(input);
  21. this.param = $.extend(this.o.param, $(input.form).serializeObject());
  22. this.init();
  23. }
  24. Autocomplete.def = {
  25. param : {//要传入脚本的参数
  26. ajax :'search_choice'//脚本名字
  27. , _isxq : 0
  28. }
  29. }
  30. Autocomplete.prototype = {
  31. init : function () {
  32. var o = this.o
  33. , $sWord = this.$sWord;
  34. var $dataBox = this.$dataBox = $('<div id="drop-data" class="drop-data"></div>').appendTo('body')
  35. .on('mouseover', 'li', function() {
  36. $(this).addClass('act').siblings().removeClass('act');
  37. })
  38. .on('mousedown', 'li', function () {
  39. $sWord.trigger('blur').trigger('pressDown', [$(this)])
  40. return false;
  41. })
  42. $sWord.attr('autocomplete', 'off')
  43. .on({
  44. keyup : $.proxy(function (e) {
  45. switch (e.which) {
  46. case 9: case 27: case 38: case 40:
  47. return false;
  48. default:
  49. var val = $.trim($sWord.val());
  50. if (this.ajaxAbort) this.ajaxAbort.abort();
  51. this.param.searchword = encodeURIComponent(val);
  52. this.ajaxAbort = $.getScript(CMS_ABS + uri2MVC($.param(this.param) + "&datatype=js&varname=data"), function() {
  53. $sWord.trigger('ajaxDone', [$dataBox, data])
  54. });
  55. return false;
  56. }
  57. }, this)
  58. , focus : function () {
  59. var $el = $sWord
  60. var oft = $el.offset();
  61. $dataBox.css({
  62. width : $el.outerWidth()
  63. , left : oft.left
  64. , top : $el.outerHeight() + oft.top
  65. , position : 'absolute'
  66. })
  67. }
  68. , blur : function () {
  69. $dataBox.css('display', 'none');
  70. }
  71. , keydown : function(e) {
  72. switch (e.which) {
  73. case 13:
  74. var $actLi = $dataBox.find('.act');
  75. if ($actLi.length) {
  76. $sWord.trigger('blur')
  77. $sWord.trigger('pressDown', [$actLi]);
  78. };
  79. return false;
  80. case 27: //esc
  81. $dataBox.css('display', 'none');
  82. return false;
  83. case 38: case 40://up down
  84. var step = e.which == 40 ? 1 : -1;
  85. var $li = $dataBox.find('li');
  86. var $actLi = $li.filter('.act').removeClass('act');
  87. var i = $actLi.length ? $actLi.index() : (step == -1 ? 0 : -1);
  88. $sWord.val($li.eq((i + step) % $li.length).addClass('act').find('.subject').text());
  89. return false;
  90. }
  91. }
  92. })
  93. }
  94. }
  95. function Plugin(option) {
  96. return this.each(function () {
  97. var $this = $(this)
  98. var data = $this.data('jqAutocomplete')
  99. var options = typeof option == 'object' && option
  100. if (!data) $this.data('jqAutocomplete', (data = new Autocomplete(this, options)))
  101. if (typeof option == 'string') data[option]()
  102. })
  103. }
  104. var old = $.fn.jqAutocomplete;
  105. $.fn.jqAutocomplete = Plugin
  106. $.fn.jqAutocomplete.Constructor = Autocomplete;
  107. $.fn.jqAutocomplete.noConflict = function () {
  108. $.fn.jqAutocomplete = old
  109. return this
  110. }
  111. $(window).load(function() {
  112. $('.jqAutocomplete').each(function(i, el) {
  113. $(this).jqAutocomplete()
  114. });
  115. });
  116. } (jQuery);
  117. // 下面是自定义的
  118. var template1 = function() {//下拉框内列表项目模板function() this:返回的数据
  119. $unit=this.dj==0 ? '待定' :(this.price_unit==1?'元/m²':'万/套');
  120. $dj=this.dj==0 ? '' : this.dj;
  121. return '<li data-url="' + this.url + '" data-aid="' + this.aid + '">'
  122. + ' <span class="subject">' + this.subject + '</span>'
  123. + ' <span class="subject" style="float:right; color:#f75f65;">' + $dj+$unit+'</span>'
  124. + '</li>'
  125. }
  126. $('.jqAutocomplete').each(function(i, el) {
  127. var caid = this.form.caid;
  128. $(this).on('ajaxDone', function (el, $dataBox, data) {
  129. var l = data.length;
  130. if (l > 0) {
  131. var html = '<ul>';
  132. for (var i = 0; i < l; i++) html += template1.call(data[i], caid);
  133. html += '</ul>';
  134. $dataBox.html(html).css('display','block');
  135. }
  136. })
  137. .on('pressDown', function (el, $li) {
  138. window.open($li.data('url'),'_self','',true)
  139. });
  140. });