<!--Part code come from :http://us.f901.mail.yahoo.com/ym/Compose?YY=123456-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Editable listbox</title>
<style>
/* Common elements */
body{font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;}
table{font-size:inherit;font:x-small;}
/* Options Templates */
.ac_menu{border:1px solid #3162A6;background-color:#F6F6F6;cursor:default;overflow:hidden;-moz-box-sizing:border-box;
height:expression((this.scrollHeight>210)?"210px":"auto")}
.ac_menuitem{width:100%;color:#141414;padding:2px;cursor:pointer;cursor:hand;}
.ac_menuitem_selected{background-color:#D6DEEC;width:100%;color:#141414;padding:2px;cursor:pointer;cursor:hand;}
/* JK */
.minHeightTextarea{
height:expression((this.scrollHeight>150)?"150px":(this.scrollHeight+5)+"px");
overflow : auto;
}
</style>
<script language=javascript>
var tofieldListArray=["深圳","bbb","ccc","ddd"];
var ccfieldListArray=tofieldListArray;
</script>
<script language=javascript>
var AC_TAB = 9; // make the selection
var AC_ENTER = 13; // make the selection
var AC_UP_ARROW = 38; // move the drop down list selection up by one
var AC_DOWN_ARROW = 40; // move the drop down list selection down by one
var theListDiv;
var theComboxObj;
οnlοad=OnLoad
function OnLoad()
{
setTimeout("Initialize()", 100);
}
function Initialize()
{
AddListDivFun(document.all.tofield);
AddListDivFun(document.all.ccfield);
}
function AddListDivFun(obj)
{
var theListArray=eval(obj.id+"ListArray");
var tempStr='<DIV style="position:relative;visibility:hidden">'
+'<DIV class=ac_menu id="'+obj.id+'ListDiv" style="FONT-SIZE: 0.9em; Z-INDEX: 1; visibility:hidden; POSITION: absolute;OVERFLOW-Y:auto; WIDTH:'+obj.offsetWidth+'; " '
+'οnmοuseοver="mouseOverListDivFun(event)" οnmοuseοut="mouseOutListDivFun(event);" οnmοusedοwn="mouseDownListDivFun(event);">';
for(var i=0;i<theListArray.length;i++)
tempStr+='<DIV class=ac_menuitem value='+i+' textValue="'+htmlEncode(theListArray[i])+'">'+htmlEncode(theListArray[i])+'</DIV>';
tempStr+="</DIV></DIV>";
obj.insertAdjacentHTML("afterEnd",tempStr);
theListDiv=eval(obj.id+"ListDiv");
obj.οnfοcus=AC_OnFocus;
obj.οnblur=AC_OnBlur;
obj.onpropertychange=AC_OnPropertyChange;
obj.οnkeydοwn=AC_OnKeyDown;
obj.autoComplete="off";
}
function AC_OnFocus(obj)
{
if(obj==null) obj=event.srcElement;
theListDiv=eval(obj.id+"ListDiv");
theComboxObj=obj;
theListDiv.style.visibility="visible";
adjustListDivScroll();
}
function AC_OnBlur(obj)
{
if(obj==null) obj=event.srcElement;
if(theListDiv.contains(document.activeElement)) obj.focus();
else theListDiv.style.visibility="hidden";
}
function AC_OnPropertyChange(obj)
{
if(theListDiv==null) return ;
if(obj==null) obj=event.srcElement;
theListDiv.selectedIndex=-1;
var theFirstVisibleIndex=-1;
var objValue=obj.value;
for(var i=0;i<theListDiv.children.length;i++)
{
if(theListDiv.children[i].textValue.indexOf(objValue)==0)
{
if(theFirstVisibleIndex==-1) theFirstVisibleIndex=i;
theListDiv.children[i].className="ac_menuitem";
theListDiv.children[i].style.display="";
theListDiv.children[i].innerHTML="<b>"+htmlEncode(objValue)+"</b>"+htmlEncode(theListDiv.children[i].textValue.substr(objValue.length));
}
else
theListDiv.children[i].style.display="none";
if(theListDiv.selectedIndex==-1 && theListDiv.children[i].textValue==objValue) theListDiv.selectedIndex=i;
}
if(theListDiv.selectedIndex==-1 && theFirstVisibleIndex!=-1)
{
theListDiv.selectedIndex=theFirstVisibleIndex;
}
if(theListDiv.selectedIndex!=-1) theListDiv.children[theListDiv.selectedIndex].className="ac_menuitem_selected";
adjustListDivScroll();
}
function AC_OnKeyDown(obj)
{
if(theListDiv==null) return ;
if(obj==null) obj=event.srcElement;
var keyCode=event.keyCode;
if(keyCode==AC_ENTER) keyCode=event.keyCode=AC_TAB;
if(keyCode==AC_TAB && theListDiv.selectedIndex!=-1)
{
obj.value=theListDiv.children[theListDiv.selectedIndex].textValue;
return ;
}
if(keyCode==AC_UP_ARROW && theListDiv.selectedIndex!=-1)
{
for(var i=theListDiv.selectedIndex-1;i>-1;i--)
{
if(theListDiv.children[i].style.display!="none")
{
theListDiv.children[theListDiv.selectedIndex].className="ac_menuitem";
theListDiv.selectedIndex=i;
theListDiv.children[theListDiv.selectedIndex].className="ac_menuitem_selected";
adjustListDivScroll();
break;
}
}
}
if(keyCode==AC_DOWN_ARROW && theListDiv.selectedIndex!=-1)
{
for(var i=theListDiv.selectedIndex*1+1;i<theListDiv.children.length;i++)
{
if(theListDiv.children[i].style.display!="none")
{
theListDiv.children[theListDiv.selectedIndex].className="ac_menuitem";
theListDiv.selectedIndex=i;
theListDiv.children[theListDiv.selectedIndex].className="ac_menuitem_selected";
adjustListDivScroll();
break;
}
}
return;
}
}
function htmlEncode(str)
{
if(str==null) return "";
str=str.replace(/</ig,"<")
str=str.replace(/>/ig,">");
str=str.replace(/"/ig,""");
return str;
}
function mouseOverListDivFun(event)
{
if(event.toElement!=theListDiv) event.toElement.style.backgroundColor="#E6E6E6";
}
function mouseOutListDivFun(event)
{
if(event.fromElement!=theListDiv) event.fromElement.style.backgroundColor="";
}
function mouseDownListDivFun(event)
{
var selectedStr="";
if(event.srcElement==theListDiv) return;
else if(event.srcElement.tagName=="B") selectedStr=event.srcElement.parentElement.textValue ;
else selectedStr=event.srcElement.textValue;
theComboxObj.value=selectedStr;
theComboxObj.blur();
}
function adjustListDivScroll()
{
if ( theListDiv==null || theListDiv.selectedIndex==-1 ) return ;
var i=theListDiv.selectedIndex;
if((theListDiv.children[i].offsetTop<theListDiv.scrollTop)||(theListDiv.children[i].offsetTop>theListDiv.scrollTop+200))
theListDiv.scrollTop=theListDiv.children[i].offsetTop-95;
}
</script>
</head>
<body >
<form name="Compose" id="Compose" method=post action="2.jsp">
<table cellpadding=4 cellspacing=0 border=0 width="100%">
<tr>
<td >Mail To:</td>
<td style="padding-top:0px;" > <textarea name="To" class="minHeightTextarea" style="width:300" tabindex="1" id="tofield"
rows="1" title="textarea" cols="20"></textarea> </td>
</tr>
<tr>
<td >Mail Cc:</td>
<td style="padding-top:0px;" > <input name="Cc" style="width:300" tabindex="1" id="ccfield" value="" title="input">
</td>
</tr>
<tr>
<td colspan="2" ><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
----------------------------
对于从数据库调下拉列表内容的方法,只需替换
var tofieldListArray=[<%
UserAuthInte uAInte = new UserAuthInte();
List funcList = uAInte.getAllFuncModels();
Iterator ite = funcList.iterator();
while (ite.hasNext() ) {
FuncModel funcM = (FuncModel) ite.next();
out.println("/""+funcM.getFunctionName()+"/",");
}%>];
var ccfieldListArray=tofieldListArray;
</script>
对于接受该下拉列表值,可用
String tofield = new String(request.getParameter("To").getBytes("ISO8859_1"),"GBK");
用name而不是ID。