emailAutoComplete.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. 
  2. /**
  3. * 邮箱自动提示插件
  4. * @constructor EmailAutoComplete
  5. * @ options {object} 可配置项
  6. */
  7. function EmailAutoComplete(options) {
  8. this.config = {
  9. targetCls : '.inputElem', // 目标input元素
  10. parentCls : '.reg-form-content', // 当前input元素的父级类
  11. hiddenCls : '.hiddenCls', // 当前input隐藏域
  12. searchForm : '.jqtransformdone', //form表单
  13. hoverBg : 'hoverBg', // 鼠标移上去的背景
  14. inputValColor : 'black', // 输入框输入提示颜色
  15. mailArr : ["@qq.com","@gmail.com","@126.com","@163.com","@hotmail.com","@yahoo.com","@sohu.com","@sina.com"], //邮箱数组
  16. isSelectHide : true, // 点击下拉框 是否隐藏 默认为true
  17. callback : function() {
  18. $('.inputElem').focus();
  19. $('.inputElem').blur();
  20. } // 点击某一项回调函数
  21. };
  22. this.cache = {
  23. onlyFlag : true, // 只渲染一次
  24. currentIndex : -1,
  25. oldIndex : -1
  26. };
  27. this.init(options);
  28. }
  29. EmailAutoComplete.prototype = {
  30. constructor: EmailAutoComplete,
  31. init: function(options){
  32. this.config = $.extend(this.config,options || {});
  33. var self = this,
  34. _config = self.config,
  35. _cache = self.cache;
  36. $(_config.targetCls).each(function(index,item){
  37. $(item).keyup(function(e){
  38. var target = e.target,
  39. targetVal = $.trim($(this).val()),
  40. keycode = e.keyCode,
  41. elemHeight = $(this).outerHeight(),
  42. elemWidth = $(this).outerWidth(),
  43. parentNode = $(this).closest(_config.parentCls);
  44. $(parentNode).css({'position':'relative'});
  45. // 如果输入框值为空的话 那么下拉框隐藏
  46. if(targetVal == '') {
  47. $(item).attr({'data-html':''});
  48. // 给隐藏域赋值
  49. $(_config.hiddenCls,parentNode).val('');
  50. _cache.currentIndex = -1;
  51. _cache.oldIndex = -1;
  52. $(".auto-tip",parentNode) && !$(".auto-tip",parentNode).hasClass('hidden') && $(".auto-tip",parentNode).addClass('hidden');
  53. self._removeBg(parentNode);
  54. }else {
  55. $(item).attr({'data-html':targetVal});
  56. // 给隐藏域赋值
  57. $(_config.hiddenCls,parentNode).val(targetVal);
  58. $(".auto-tip",parentNode) && $(".auto-tip",parentNode).hasClass('hidden') && $(".auto-tip",parentNode).removeClass('hidden');
  59. // 渲染下拉框内容
  60. self._renderHTML({keycode:keycode,e:e,target:target,targetVal:targetVal,height:elemHeight,width:elemWidth,parentNode:parentNode});
  61. }
  62. });
  63. });
  64. // 阻止form表单默认enter键提交
  65. $(_config.searchForm).each(function(index,item) {
  66. $(item).keydown(function(e){
  67. var keyCode = e.keyCode;
  68. if(keyCode == 13) {
  69. return false;
  70. }
  71. });
  72. });
  73. // 点击文档document时候 下拉框隐藏掉
  74. $(document).click(function(e){
  75. e.stopPropagation();
  76. var target = e.target,
  77. tagCls = _config.targetCls.replace(/^\./,'');
  78. if(!$(target).hasClass(tagCls)) {
  79. $('.auto-tip') && $('.auto-tip').each(function(index,item){
  80. !$(item).hasClass('hidden') && $(item).addClass('hidden');
  81. });
  82. }
  83. });
  84. },
  85. /*
  86. * 渲染下拉框提示内容
  87. * @param cfg{object}
  88. */
  89. _renderHTML: function(cfg) {
  90. var self = this,
  91. _config = self.config,
  92. _cache = self.cache,
  93. curVal;
  94. var curIndex = self._keyCode(cfg.keycode);
  95. $('.auto-tip',cfg.parentNode).hasClass('hidden') && $('.auto-tip',cfg.parentNode).removeClass('hidden');
  96. if(curIndex > -1){
  97. // 键盘上下操作
  98. self._keyUpAndDown(cfg.targetVal,cfg.e,cfg.parentNode);
  99. }else {
  100. if(/@/.test(cfg.targetVal)) {
  101. curVal = cfg.targetVal.replace(/@.*/,'');
  102. }else {
  103. curVal = cfg.targetVal;
  104. }
  105. if(_cache.onlyFlag) {
  106. $(cfg.parentNode).append('<input type="hidden" class="hiddenCls"/>');
  107. var wrap = '<ul class="auto-tip">';
  108. for(var i = 0; i < _config.mailArr.length; i++) {
  109. wrap += '<li class="p-index'+i+'">'+'<span class="output-num"></span><em class="em" data-html="'+_config.mailArr[i]+'">'+_config.mailArr[i]+'</em></li>';
  110. }
  111. wrap += '</ul>';
  112. _cache.onlyFlag = false;
  113. $(cfg.parentNode).append(wrap);
  114. $('.auto-tip',cfg.parentNode).css({'position':'absolute','top':cfg.height,'width':cfg.width - 2 + 'px','left':0,
  115. 'border':'1px solid #ccc','z-index':10000});
  116. }
  117. // 给所有li添加属性 data-html
  118. $('.auto-tip li',cfg.parentNode).each(function(index,item){
  119. $('.output-num',item).html(curVal);
  120. !$('.output-num',item).hasClass(_config.inputValColor) &&
  121. $('.output-num',item).addClass(_config.inputValColor);
  122. var emVal = $.trim($('.em',item).attr('data-html'));
  123. $(item).attr({'data-html':curVal + '' +emVal});
  124. });
  125. // 精确匹配内容
  126. self._accurateMate({target:cfg.target,parentNode:cfg.parentNode});
  127. // 鼠标移到某一项li上面时候
  128. self._itemHover(cfg.parentNode);
  129. // 点击对应的项时
  130. self._executeClick(cfg.parentNode);
  131. }
  132. },
  133. /**
  134. * 精确匹配某项内容
  135. */
  136. _accurateMate: function(cfg) {
  137. var self = this,
  138. _config = self.config,
  139. _cache = self.cache;
  140. var curVal = $.trim($(cfg.target,cfg.parentNode).attr('data-html')),
  141. newArrs = [];
  142. if(/@/.test(curVal)) {
  143. // 获得@ 前面 后面的值
  144. var prefix = curVal.replace(/@.*/, ""),
  145. suffix = curVal.replace(/.*@/, "");
  146. $.map(_config.mailArr,function(n){
  147. var reg = new RegExp(suffix);
  148. if(reg.test(n)) {
  149. newArrs.push(n);
  150. }
  151. });
  152. if(newArrs.length > 0) {
  153. $('.auto-tip',cfg.parentNode).html('');
  154. $(".auto-tip",cfg.parentNode) && $(".auto-tip",cfg.parentNode).hasClass('hidden') &&
  155. $(".auto-tip",cfg.parentNode).removeClass('hidden');
  156. var html = '';
  157. for(var j = 0, jlen = newArrs.length; j < jlen; j++) {
  158. html += '<li class="p-index'+j+'">'+'<span class="output-num"></span><em class="em" data-html="'+newArrs[j]+'">'+newArrs[j]+'</em></li>';
  159. }
  160. $('.auto-tip',cfg.parentNode).html(html);
  161. // 给所有li添加属性 data-html
  162. $('.auto-tip li',cfg.parentNode).each(function(index,item){
  163. $('.output-num',item).html(prefix);
  164. !$('.output-num',item).hasClass(_config.inputValColor) &&
  165. $('.output-num',item).addClass(_config.inputValColor);
  166. var emVal = $.trim($('.em',item).attr('data-html'));
  167. $(item).attr('data-html','');
  168. $(item).attr({'data-html':prefix + '' +emVal});
  169. });
  170. // 精确匹配到某项时候 让当前的索引等于初始值
  171. _cache.currentIndex = -1;
  172. _cache.oldIndex = -1;
  173. $('.auto-tip .output-num',cfg.parentNode).html(prefix);
  174. // 鼠标移到某一项li上面时候
  175. self._itemHover(cfg.parentNode);
  176. // 点击对应的项时
  177. self._executeClick(cfg.parentNode);
  178. }else {
  179. $(".auto-tip",cfg.parentNode) && !$(".auto-tip",cfg.parentNode).hasClass('hidden') &&
  180. $(".auto-tip",cfg.parentNode).addClass('hidden');
  181. $('.auto-tip',cfg.parentNode).html('');
  182. }
  183. }
  184. },
  185. /*
  186. * 鼠标移到某一项li上时
  187. */
  188. _itemHover: function(parentNode) {
  189. var self = this,
  190. _config = self.config,
  191. _cache = self.cache;
  192. $('.auto-tip li',parentNode).hover(function(index,item) {
  193. !$(this).hasClass(_config.hoverBg) && $(this).addClass(_config.hoverBg);
  194. },function() {
  195. $(this).hasClass(_config.hoverBg) && $(this).removeClass(_config.hoverBg);
  196. });
  197. },
  198. /*
  199. * 当输入框值为空时候 li项都删掉class hoverBg
  200. */
  201. _removeBg: function(parentNode){
  202. var self = this,
  203. _config = self.config;
  204. $(".auto-tip li",parentNode).each(function(index,item){
  205. $(item).hasClass(_config.hoverBg) && $(item).removeClass(_config.hoverBg);
  206. });
  207. },
  208. /**
  209. * 键盘上下键操作
  210. */
  211. _keyUpAndDown: function(targetVal,e,parentNode) {
  212. var self = this,
  213. _cache = self.cache,
  214. _config = self.config;
  215. // 如果请求成功后 返回了数据(根据元素的长度来判断) 执行以下操作
  216. if($('.auto-tip' + ' li',parentNode) && $('.auto-tip' + ' li').length > 0) {
  217. var plen = $('.auto-tip' + ' li',parentNode).length,
  218. keyCode = e.keyCode;
  219. _cache.oldIndex = _cache.currentIndex;
  220. // 上移操作
  221. if(keyCode == 38) {
  222. if(_cache.currentIndex == -1) {
  223. _cache.currentIndex = plen - 1;
  224. }else {
  225. _cache.currentIndex = _cache.currentIndex - 1;
  226. if(_cache.currentIndex < 0) {
  227. _cache.currentIndex = plen - 1;
  228. }
  229. }
  230. if(_cache.currentIndex !== -1) {
  231. !$('.auto-tip .p-index'+_cache.currentIndex,parentNode).hasClass(_config.hoverBg) &&
  232. $('.auto-tip .p-index'+_cache.currentIndex,parentNode).addClass(_config.hoverBg).siblings().removeClass(_config.hoverBg);
  233. var curAttr = $('.auto-tip' + ' .p-index'+_cache.currentIndex,parentNode).attr('data-html');
  234. $(_config.targetCls,parentNode).val(curAttr);
  235. // 给隐藏域赋值
  236. $(_config.hiddenCls,parentNode).val(curAttr);
  237. }
  238. }else if(keyCode == 40) { //下移操作
  239. if(_cache.currentIndex == plen - 1) {
  240. _cache.currentIndex = 0;
  241. }else {
  242. _cache.currentIndex++;
  243. if(_cache.currentIndex > plen - 1) {
  244. _cache.currentIndex = 0;
  245. }
  246. }
  247. if(_cache.currentIndex !== -1) {
  248. !$('.auto-tip .p-index'+_cache.currentIndex,parentNode).hasClass(_config.hoverBg) &&
  249. $('.auto-tip .p-index'+_cache.currentIndex,parentNode).addClass(_config.hoverBg).siblings().removeClass(_config.hoverBg);
  250. var curAttr = $('.auto-tip' + ' .p-index'+_cache.currentIndex,parentNode).attr('data-html');
  251. $(_config.targetCls,parentNode).val(curAttr);
  252. // 给隐藏域赋值
  253. $(_config.hiddenCls,parentNode).val(curAttr);
  254. }
  255. }else if(keyCode == 13) { //回车操作
  256. var curVal = $('.auto-tip' + ' .p-index'+_cache.oldIndex,parentNode).attr('data-html');
  257. $(_config.targetCls,parentNode).val(curVal);
  258. // 给隐藏域赋值
  259. $(_config.hiddenCls,parentNode).val(curVal);
  260. if(_config.isSelectHide) {
  261. !$(".auto-tip",parentNode).hasClass('hidden') && $(".auto-tip",parentNode).addClass('hidden');
  262. }
  263. _config.callback && $.isFunction(_config.callback) && _config.callback();
  264. _cache.currentIndex = -1;
  265. _cache.oldIndex = -1;
  266. }
  267. }
  268. },
  269. _keyCode: function(code) {
  270. var arrs = ['17','18','38','40','37','39','33','34','35','46','36','13','45','44','145','19','20','9'];
  271. for(var i = 0, ilen = arrs.length; i < ilen; i++) {
  272. if(code == arrs[i]) {
  273. return i;
  274. }
  275. }
  276. return -1;
  277. },
  278. /**
  279. * 当数据相同的时 点击对应的项时 返回数据
  280. */
  281. _executeClick: function(parentNode) {
  282. var _self = this,
  283. _config = _self.config;
  284. $('.auto-tip' + ' li',parentNode).unbind('click');
  285. $('.auto-tip' + ' li',parentNode).bind('click',function(e){
  286. var dataAttr = $(this).attr('data-html');
  287. $(_config.targetCls,parentNode).val(dataAttr);
  288. if(_config.isSelectHide) {
  289. !$(".auto-tip",parentNode).hasClass('hidden') && $(".auto-tip",parentNode).addClass('hidden');
  290. }
  291. // 给隐藏域赋值
  292. $(_config.hiddenCls,parentNode).val(dataAttr);
  293. _config.callback && $.isFunction(_config.callback) && _config.callback();
  294. });
  295. }
  296. };
  297. // 初始化
  298. $(function() {
  299. new EmailAutoComplete({});
  300. });