简单JS自动提示文本框
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 675
为了满足用户的方便,提供良好的自动提示功能就成为定级公司努力方向。废话少说,看例子。本例以颜色为例,为用户提供自动提示,展示效果和运行结果如图:1、建立框架结构:<body><formm

为了满足用户的方便,提供良好的自动提示功能就成为定级公司努力方向。废话少说,看例子。
本例以颜色为例,为用户提供自动提示,展示效果和运行结果如图:

 

1、建立框架结构:

<body>
<form method="post" name="myForm1">
Color: <input type="text" name="colors" id="colors" onkeyup="findColors();" />
</form>
<div id="popup">
 <ul id="colors_ul"></ul>
</div>
</body>

2、页面和表单CSS:

<style>
<!--
body{
 font-family:Arial, Helvetica, sans-serif;
 font-size:12px; padding:0px; margin:5px; form{padding:0px; margin:0px;}
input{  font-family:Arial, Helvetica, sans-serif;
 font-size:12px; border:1px solid #000000;
 width:200px; padding:1px; margin:0px; #popup{  position:absolute; width:202px;
 color:#004a7e; font-size:12px;
 font-family:Arial, Helvetica, sans-serif;
 left:41px; top:25px; #popup.show{  border:1px solid #004a7e; #popup.hide{  border:none;
} ul{
 list-style:none;
 margin:0px; padding:0px; li.mouseOver{
 background-color:#004a7e;
 color:#FFFFFF; li.mouseOut{
 background-color:#FFFFFF;
 color:#004a7e; -->
</style>

3、实现JS匹配用户输入:

<script language="javascript">
var oInputField; //考虑到很多函数中都要使用
var oPopDiv;  //因此采用全局变量的形式
var oColorsUl;
var aColors = ["red","green","blue","magenta","yellow",……"ivory","darkmagenta","cornfloewrblue"];
aColors.sort(); //按字母排序,使显示结果更友好
function initVars(){  oInputField = document.forms["myForm1"].colors;
 oPopDiv = document.getElementByIdx_x("popup");
 oColorsUl = document.getElementByIdx_x("colors_ul"); function clearColors(){  for(var i=oColorsUl.childNodes.length-1;i>=0;i--)
  oColorsUl.removeChild(oColorsUl.childNodes[i]);
 oPopDiv.className = "hide"; function setColors(the_colors){  clearColors(); //每输入一个字母就先清除原先的提示,再继续
 oPopDiv.className = "show";
 var oLi;
 for(var i=0;i<the_colors.length;i++){   oLi = document.createElement_x("li");
  oColorsUl.appendChild(oLi);
  oLi.appendChild(document.createTextNode(the_colors[i]));

  oLi. = function(){
   this.className = "mouseOver"; //鼠标经过时高亮   oLi. = function(){
   this.className = "mouseOut"; //离开时恢复原样   oLi.onclick = function(){    oInputField.value = this.firstChild.nodeValue;
   clearColors(); //同时清除提示框  } function findColors(){
 initVars();  //初始化变量
 if(oInputField.value.length > 0){
  var aResult = new Array();  //用于存放匹配结果
  for(var i=0;i<aColors.length;i++) //从颜色表中找匹配的颜色    if(aColors[i].indexOf(oInputField.value) == 0)
    aResult.push(aColors[i]); //压入结果
  if(aResult.length>0) //如果有匹配的颜色则显示出来
   setColors(aResult);
  else     //否则清除,用户多输入一个字母
   clearColors();  //就有可能从有匹配到无,到无的时候需要清除  else
  clearColors(); //无输入时清除提示框(例如用户按del键) </script>

我的问题网 www.shenzhidao.com 有关于网站的东西欢迎大家一起交流!
联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2