这篇文章主要介绍了js select支持手动输入实现代码,需要的朋友可以参考下
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 手动输入内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:js select支持手动输入功能实现代码


基础教程推荐
猜你喜欢
- JavaScript垃圾回收机制(引用计数,标记清除,性能优 2022-08-31
- Unicode中的常用字母小结 2022-09-21
- Ajax提交表单并接收json实例代码 2023-02-13
- 在IE中为abbr标签加样式 2022-10-16
- AJax 把拿到的后台数据在页面中渲染的实例 2023-02-22
- ajax实现数据分页查询 2023-01-31
- 关于ajax异步访问数据的问题 2023-02-23
- 原生ajax瀑布流demo分享(必看篇) 2023-02-01
- 解决:layUI数据表格+简单查询 2022-12-16
- 纯javascript的ajax实现php异步提交表单的简单实例 2022-12-28