---
title: "可输入select选单"
author: "fanjava"
date: "2004-09-17T08:13:00Z"
tags: []
slug: "select"
draft: false
lastmod: "2004-09-17T08:13:00Z"
id: 601898
url: "https://fanjava.iteye.com/blog/601898"
---

&lt;html&gt;  
&lt;head&gt;  
&lt;style&gt;  
&lt;!--  
.cls1 { position:absolute; left:250px; top:89px; width:216px; height:72px; z-index:1; }  
.cls2 { position:absolute; left:250px; top:89px; width:95px; height:18px; z-index:2 }  
input { font-size: 12px; padding-top: 2px; padding-left: 2px;width:127;}  
//--&gt;  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;script&gt;  
&lt;!--  
function addOption(pos){

if(event.keyCode==13)  
{  
var select\_obj = document.getElementById("myselect");  
var text\_value = document.getElementById("test").value;

if (select\_obj.length)  
{  
for (var i=0;i&lt;select\_obj.length;i++)  
{  
// 如果已经存在，不添加，直接退出  
if (select\_obj.options\[i].text==text\_value)  
alert("选项已存在，请重新输入");  
}  
}  
var the\_option= new Option(text\_value,text\_value);

select\_obj.add(the\_option);

select\_obj.selectedIndex = select\_obj.options.length-1  
}

}  
//--&gt;  
&lt;/script&gt;  
&lt;div class="cls1" style="clip: rect(3 280 21 110)"&gt;  
&lt;select name="myselect" style="width:127" onchange="document.getElementById('test').value=this.value"&gt;  
&lt;/select&gt;  
&lt;/div&gt;  
&lt;div class="cls2"&gt;  
&lt;input type="text" name="test" onkeydown="addOption()" size="20" style="width: 127; height: 23"&gt;  
&lt;/div&gt;

&lt;/body&gt;  
&lt;/html&gt;