可编辑的下拉菜单

 

                    

 

  1. 一。
  2. /**
  3.  *可编辑下拉框对象类
  4.  *
  5.  *可编辑下拉框,其实就是在下拉框的上面放了个无边框的输入框,在输入框输数据时,下拉框自动定位,
  6.  *下拉框选中某项时,下拉框的值赋给输入框,从而产生错觉
  7.  *
  8.  */
  9. function combox(_inpuObjName,_controlSelectName)
  10. {
  11.     //生成的输入框对象名称
  12.     this.inpuObjName=_inpuObjName;
  13.  //生成的输入框对象
  14.  this.inputbox=null;
  15.     //原来的下拉框对象
  16.     this.controlSelect=document.getElementById(_controlSelectName);
  17.     //初始化对象
  18.  //_comboxObj:combox对象,须指向自己
  19.     this.init=function(_comboxObj)
  20.     {
  21.   this.inputbox=document.createElement("input");
  22.   thisthis.inputbox.id=this.inpuObjName;
  23.   this.inputbox.comboxObj=_comboxObj;
  24.   this.inputbox.onchange=function()
  25.   {
  26.    this.comboxObj.find();
  27.   }
  28.   with(this.inputbox.style)
  29.   {
  30.    width=this.controlSelect.offsetWidth-16;
  31.    height=this.controlSelect.offsetHeight;
  32.   }  
  33.   this.controlSelect.insertAdjacentElement("beforeBegin",this.inputbox);
  34.         _span=document.createElement("span");
  35.         _span.style.width=18;
  36.         this.controlSelect.insertAdjacentElement("beforeBegin",_span);
  37.         _span.appendChild(this.controlSelect);
  38.         _container=document.createElement("span");
  39.         this.inputbox.insertAdjacentElement("beforeBegin",_container);
  40.         _container.appendChild(this.inputbox);
  41.         _container.appendChild(_span);
  42.         _container.style.width=this.inputbox.offsetWidth+18;
  43.         _width=this.controlSelect.offsetWidth-18;
  44.         with (this.controlSelect.style)
  45.         {
  46.             margin="0 0 0 -"+_width;
  47.         }
  48.   this.controlSelect.comboxObj=_comboxObj;
  49.         this.controlSelect.onchange=function()
  50.   {
  51.    this.comboxObj.change();   
  52.   }
  53.         this.change();
  54.     }
  55.     //当搜索到输入框的值时,下拉框自动定位/
  56.     this.find=function()
  57.     {
  58.         with (this.controlSelect)
  59.         {
  60.             for(i=0;i<options.length;i++)
  61.                 if(options[i].text.indexOf(this.inputbox.value)==0)
  62.                 {
  63.                     selectedIndex=i;
  64.                     this.change();
  65.                     break;
  66.                 }
  67.         }
  68.     }
  69.     //定义下拉框的onchange事件
  70.     this.change=function()
  71.     {        
  72.         thisthis.inputbox.value=this.controlSelect.options[this.controlSelect.selectedIndex].text;
  73.         with (this.inputbox)
  74.         {
  75.             select();
  76.             focus();
  77.         }
  78.     }
  79. }
  80. /**
  81.  * 定位函数,获取控件绝对坐标
  82.  */
  83. function getLeftPos(e)
  84. {
  85.     var left=e.offsetLeft;
  86.     while (ee=e.offsetParent)
  87.     {
  88.         left+=e.offsetLeft;
  89.     }
  90.     return left;
  91. }
  92. function getTopPos(e)
  93. {
  94.     var top=e.offsetTop;
  95.     while (ee=e.offsetParent)
  96.     {
  97.         top+=e.offsetTop;
  98.     }
  99.     return top;
  100. }  
  101. //可编辑下拉框测试页面
  102. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  103. <HTML>
  104. <HEAD>
  105. <TITLE> New Document </TITLE>
  106. <META NAME="Generator" CONTENT="EditPlus">
  107. <META NAME="Author" CONTENT="">
  108. <META NAME="Keywords" CONTENT="">
  109. <META NAME="Description" CONTENT="">
  110. <script language="javascript" src="combox.js"></script>
  111. </HEAD>
  112. <BODY>
  113. <table border="1" style="border-collapse:collapse ">
  114. 下拉框:<td width="300"><select name=test1>
  115.   <option value="可编辑下拉框1" selected>可编辑下拉框1</option>
  116.   <option value="1">1</option>
  117.   <option value="2">2</option>
  118.    <option value="3">3</option>
  119.   </select>
  120.  <INPUT TYPE="button" value="获取编辑框1的值" onclick="alert(document.getElementById('username').value)">
  121.   </td>
  122.     <!--<td width="109"><select name=test2>
  123.       <option value="可编辑下拉框2">可编辑下拉框25555555</option>
  124.       <option value="1">4</option>
  125.       <option value="2" selected>5</option>
  126.       <option value="3">6</option>
  127.     </select> 
  128.  </td>
  129.     <td width="343">
  130.   <INPUT TYPE="button" value="获取编辑框2的值" onclick="alert(document.getElementById('password').value)">
  131.  </td>-->
  132.   
  133. </table>
  134. <script language="javascript">
  135.  var a=new combox("username","test1");
  136.  //参数1为新生成输入框的名称
  137.  //参数2为原来的select对象名称
  138.  a.init(a);
  139. // var b=new combox("password","test2");
  140. // b.init(b);
  141. </script>
  142. </BODY>
  143. </HTML> 

二。

/**

来个更简单的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="gb2312" />
<title>可选择可输入的下拉框</title>
<style type="text/css">
.combobx {width:100px;overflow:hidden;position:relative;padding:0;display:inline;}
.combobx input {width:80px;position:absolute;left:0;top:0;}
.combobx select {width:100px;position:absolute;left:0;top:0;clip:rect(auto auto auto 82px);}
</style>
</head>
<body>
<form method="post" action="#">
学历:
<div class="combobx"><input type="text" name="xxx">
<select οnchange="this.parentNode.childNodes[0].value=this.value;">
<option value="小学">小学</option>
<option value="中学">中学</option>
<option value="大学">大学</option>
</select>
</div>
</form>
</body>
</html>

*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值