例1:
<html>
<head>
<script language="javascript">
<!--
//定义 select 原值
var oldValue,oldText;
//select下拉框的onkeydown事件,修改下拉框的值
function catch_keydown(sel){
switch(event.keyCode) {
case 13: //回车键
event.returnValue = false;
break;
case 27: //Esc键
sel.options[sel.selectedIndex].text = oldText;
sel.options[sel.selectedIndex].value = oldValue;
event.returnValue = false;
break;
case 8://空格健
var s = sel.options[sel.selectedIndex].text;
s = s.substr(0,s.length-1);
if (sel.options[0].value==sel.options[sel.selectedIndex].text){
sel.options[sel.selectedIndex].value=s;
sel.options[sel.selectedIndex].text=s;
}
event.returnValue = false;
break;
} // www.jb200.com
if (!event.returnValue && sel.onchange)
sel.onchange(sel)
}
//select下拉框的onkeypress事件,修改下拉框的值
function catch_press(sel){
if(sel.selectedIndex>=0){
var s = sel.options[sel.selectedIndex].text + String.fromCharCode(event.keyCode);
if (sel.options[sel.selectedIndex].value==sel.options[sel.selectedIndex].text){
sel.options[sel.selectedIndex].value=s;
sel.options[sel.selectedIndex].text=s;
}
event.returnValue = false;
if (!event.returnValue && sel.onchange)
sel.onchange(sel)
}
}
//select下拉框的onfocus事件,保存下拉框原来的值
function catch_focus(sel) {
oldText = sel.options[sel.selectedIndex].value;
oldValue = sel.options[sel.selectedIndex].value;
}
//-->
</script>
</head>
<body>
<!--调用-->
<select style='width:130px;z-index:-1' name='tmpSel'onkeydown=catch_keydown(this) onkeypress=catch_press(this) onfocus=catch_focus(this)>
<option value=''></option>
<option value=''>A</option>
<option value=''>B</option>
<option value=''>C</option>
</select>
</body>
</html>
例2,带有提示信息的html select表单:
<html>
<head>
<script language="javascript">
<!--
//定义 select 原值
var oldValue,oldText;
//select下拉框的onkeydown事件,修改下拉框的值
function catch_keydown(sel){
switch(event.keyCode) {
case 13: //回车键
event.returnValue = false;
break;
case 27: //Esc键
sel.options[sel.selectedIndex].text = oldText;
sel.options[sel.selectedIndex].value = oldValue;
event.returnValue = false;
break;
case 8://空格健
var s = sel.options[sel.selectedIndex].text;
s = s.substr(0,s.length-1);
if (sel.options[sel.selectedIndex].value==sel.options[sel.selectedIndex].text){
sel.options[sel.selectedIndex].value=s;
sel.options[sel.selectedIndex].text=s;
}
event.returnValue = false;
break;
} // www.jb200.com
if (!event.returnValue && sel.onchange)
sel.onchange(sel)
}
//select下拉框的onkeypress事件,修改下拉框的值
function catch_press(sel){
if(sel.selectedIndex>=0){
var s = sel.options[sel.selectedIndex].text + String.fromCharCode(event.keyCode);
if (sel.options[sel.selectedIndex].value==sel.options[sel.selectedIndex].text){
sel.options[sel.selectedIndex].value=s;
sel.options[sel.selectedIndex].text=s;
}
event.returnValue = false;
if (!event.returnValue && sel.onchange)
sel.onchange(sel)
}
}
//select下拉框的onfocus事件,保存下拉框原来的值
function catch_focus(sel) {
oldText = sel.options[sel.selectedIndex].value;
oldValue = sel.options[sel.selectedIndex].value;
}
//select 选择框鼠标上移时提示选择的内容
function selMouseOver(obj){
with (document.all.div_hint){
innerText = obj.options[obj.selectedIndex].text;
if (innerText.length > 0){
innerText = " " + innerText + "";
style.display = "block";
style.left = event.clientX + 16;
style.top = event.clientY;
}
}
}
//select 选择框鼠标移开时消失
function selMouseOut(obj){
with (document.all.div_hint){
style.display = "none"
}
}
//-->
</script>
</head>
<body>
<!--调用-->
<select style='width:130px;z-index:-1' name='tmpsel' onmouseover=selmouseover(this) onmouseout=selmouseout(this) onkeydown=catch_keydown(this) onkeypress=catch_press(this) onfocus=catch_focus(this)>
<option value=''></option>
</select>
<!--提示块-->
<div id=div_hint style="font-size:12px;color:red;display:none;position:absolute; z-index:2; top:200;background-color: #f7f7f7; layer-background-color: #0099ff; border: 1px #9c9c9c solid;filter:alpha(style=0,opacity=80,finishopacity=100);"></div>
</body>
</html>