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;
}
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 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>
到此这篇关于js select支持手动输入功能实现代码的文章就介绍到这了,更多相关js select 手动输入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章:
1. JavaScript实现获取图片文件真实格式的示例代码2. requestAnimationFrame使用示例详解3. JavaScript开发中需要搞懂的字符编码总结4. 高频率Vue面试题汇总以及答案5. vue3使用el-upload上传文件示例详解6. JavaScript深拷贝方法structuredClone使用7. uniapp 手机验证码输入框实现代码(随机数、倒计时、隐藏手机号码中间四位)可以直接使用8. 使用Node.js实现Clean Architecture方法示例详解9. 使用uniapp打包微信小程序时主包和vendor.js过大解决(uniCloud的插件分包)10. JS实现微信播音效果示例详解