本文实例讲述了jQuery实现输入框邮箱内容自动补全与上下翻动显示效果。分享给大家供大家参考,具体如下:

最近在做通行证项目,里面注册模块有邮箱注册,需求方想要在输入 @ 后触发下拉框显示各个邮箱,效果如下:

jQuery实现输入框邮箱内容自动补全与上下翻动显示效果【附demo源码下载】

html 代码:

<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <title>邮箱自动补全</title>
  <link rel="stylesheet" type="text/css" href="autoComplete.css" media="all"/>
</head>
<body>
  <h1>邮箱自动补全 + 上下翻动</h1>
  <p>当在输入框内输入 @ 时,自动显示各个邮箱的下拉列表。</p>
  <div class="wrap">
    <form action="result.php" method="post">
      <input type="text" name="email" id="email" class="inp" autocomplete="off"/><br/><br/>
      <input type="text" name="other" class="inp" autocomplete="off"/><br/><br/>
      <input type="submit" value="提交表单" id="submit"/>
    </form>
  </div>
</body>
</html>
<script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.min.js">

css 代码:

@charset 'utf-8';
.wrap{width:200px;margin:0 auto;}
h1{font-size:36px;text-align:center;line-height:60px;}
p{font-size:20px;text-align:center;line-height:60px;}
.inp{width:190px;border:1px solid #ccc;border-radius:5px;height:30px;line-height:30px;padding:5px;}
#AutoComplete{background:#fff;border:1px solid #4190db;display:none;width:200px;}
#AutoComplete ul{list-style-type:none;margin:0;padding:0;}
#AutoComplete li{color:#333;cursor:pointer;font:12px/22px \5b8b\4f53;text-indent:5px;}
#AutoComplete .hover{background:#6eb6fe;color:#fff;}

js 代码:

jQuery.AutoComplete = function(selector){
  var elt = $(selector);
  var autoComplete,autoLi;
  var strHtml = [];
  strHtml.push('<div class="AutoComplete" id="AutoComplete">');
  strHtml.push('  <ul class="AutoComplete_ul">');
  strHtml.push('    <li class="AutoComplete_title">请选择邮箱后缀</li>');
  strHtml.push('    <li hz="@qq.com"></li>');
  strHtml.push('    <li hz="@163.com"></li>');
  strHtml.push('    <li hz="@126.com"></li>');
  strHtml.push('    <li hz="@sohu.com"></li>');
  strHtml.push('    <li hz="@sina.com"></li>');
  strHtml.push('  </ul>');
  strHtml.push('</div>');
  $('body').append(strHtml.join(''));
  autoComplete = $('#AutoComplete');
  autoComplete.data('elt',elt);
  autoLi = autoComplete.find('li:not(.AutoComplete_title)');
  autoLi.mouseover(function(){
    $(this).siblings().filter('.hover').removeClass('hover');
    $(this).addClass('hover');
  }).mouseout(function(){
    $(this).removeClass('hover');
  }).mousedown(function(){
    autoComplete.data('elt').val($(this).text()).change();
    autoComplete.hide();
  });
  //用户名补全+翻动
  elt.keyup(function(e){
    if(/13|38|40|116/.test(e.keyCode) || this.value == ''){
      return false;
    }
    var username = this.value;
    if(username.indexOf('@') == -1){
      autoComplete.hide();
      return false;
    }
    autoLi.each(function(){
      this.innerHTML = username.replace(/\@+.*/,'') + $(this).attr('hz');
      if(this.innerHTML.indexOf(username) >= 0){
        $(this).show();
      }else{
        $(this).hide();
      }
    }).filter('.hover').removeClass('hover');
    autoComplete.show().css({
      left: $(this).offset().left,
      top: $(this).offset().top + $(this).outerHeight(true) - 1,
      position: 'absolute',
      zIndex: '99999'
    });
    if(autoLi.filter(':visible').length == 0){
      autoComplete.hide();
    }else{
      autoLi.filter(':visible').eq(0).addClass('hover');
    }
  }).keydown(function(e){
    if(e.keyCode == 38){ //上
      autoLi.filter('.hover').prev().not('.AutoComplete_title').addClass('hover').next().removeClass('hover');
    }else if(e.keyCode == 40){ //下
      autoLi.filter('.hover').next().addClass('hover').prev().removeClass('hover');
    }else if(e.keyCode == 13){ //Enter
      autoLi.filter('.hover').mousedown();
      e.preventDefault();  //如有表单,阻止表单提交
    }
  }).focus(function(){
    autoComplete.data('elt',$(this));
  }).blur(function(){
    autoComplete.hide();
  });
}

result.php

<"<br/>" . $_POST['other'];
"_blank" href="http://xiazai.jb51.net/201609/yuanma/jquery-auto-complete-plugs-codes(jb51.net).rar">本站下载。

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery常用插件及用法总结》、《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。