可以编辑的Select (第二版)

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 bencalie 的 blog

观看效果

<script src="editableselect.js"></script>
<select id="editableselect">
<option>可以编辑的select第二版</option>
<option>bencalie制作</option>
</select>

=======================
editableselect.js

window.onload=function(){
 var objselect=editableselect
 var obj=document.all.editableselect
 gettop=obj.offsettop;
 getleft=obj.offsetleft;
 while(objselect=objselect.offsetparent){
 gettop+=objselect.offsettop;
 getleft+=objselect.offsetleft;
 }

 var onewitem=document.createelement("object"); 
 document.body.insertbefore(onewitem);
 onewitem.outerhtml="<object id=editable style=\"z-index:2;position:absolute\" type=\"text/x-scriptlet\" data=\"addin2.htm\"></object>";

 editable.style.left=getleft+1
 editable.style.top=gettop+1
 editable.style.width=obj.offsetwidth-19
 editable.style.height=obj.offsetheight-3
 
 obj.onchange=function(){editable.str(obj.options[obj.selectedindex].text)}
 obj.onresize=function(){editable.style.width=obj.offsetwidth-19}
}

function addnewoption(value){
 editableselect.options[editableselect.length]=new option(value,value)
}

=======================
addin2.htm

<script language="vbs">
function public_str(thestrin)
 strin.value=thestrin
end function
</script>
<body leftmargin=0 topmargin=0>
<script language="javascript">
function check(){
var obj=parent.document.all.editableselect
var thevalue=document.all.strin.value.replace(/^\s*/g,"").replace(/\s*$/g,"")
if(event.keycode==13){
if(thevalue!=""){
for(i=0;i<obj.length;i++)
 if(obj.options[i].text==thevalue){
  alert("该选项已经存在!");
  document.all.strin.focus();
  document.all.strin.value="";
  return;
 }
parent.addnewoption(thevalue) 
}
document.all.strin.value=""
}
}
</script>
<input id=strin style='border:0;width:100%;height:100%;padding-top:2px' onkeydown=check()>
</body>

本文关键:Select
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top