<div class="codetitle"> <span><a style="CURSOR: pointer" data="32105" class="copybut" id="copybut32105" onclick="doCopy('code32105')"><u>複製程式碼</u></a></span> 代碼如下:</div> <div class="codebody" id="code32105"> <br><select id="name" name="name"> onkeydown="clearSelect(this,event);" <br>onkeypress="writeSelect(this,event);" style="width:70px;"> <br><option value=""></option> <br><option value="test1">test1</option> <br><option value="test2">test2</option> <br><option value="test3">test3</option> <br> <br><br><script> <BR>function clearSelect(obj,e) <BR>{ <BR>opt = obj.options[0]; <BR>opt.selected = "selected"; 🎜>if((e.keyCode== 8) ||(e.charCode==8))//使用退格(backspace)鍵實作逐字刪除的編輯功能<BR>{ <BR>opt.value = opt.value.substring(0, opt.value.length>0?opt.value.length-1:0); <BR>opt.text = opt.value; <BR>} <BR>if((e. keyCode== 46) ||(e.charCode==46))//使用刪除(Delete)鍵實現逐字刪除的編輯功能<BR>{ <BR>opt.value = ""; <BR>opt. text = opt.value; <BR>} <BR>//還可以實現其他按鍵的回應<BR>} <BR><br>function writeSelect(obj,e) <br>{ <BR>opt = obj. options[0]; <BR>opt.selected = "selected"; <BR>opt.value = String.fromCharCode(e.charCode <🎜); <BR>opt.text = opt.value;||e.keyCode); <BR>opt.text = opt.value; >} <BR>function forbidBackSpace()//為了在IE中,避免backspace的返回上一頁功能,和本下拉框的編輯功能衝突,需要禁掉backspace的功能。 forbidBackSpace可以寫在<body onkeydown="forbidBackSpace();">中。 <BR>{ <BR>if((event.keyCode == 8) && (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password ")) <BR>{ <BR>event.keyCode = 0; <BR>event.returnValue = false; <BR>} <BR>} <BR></script> <br><br></select> </div>