为select的option绑定键盘事件
1. 目的
可以使用快捷键1、2、3、4等自动选中select框对应的option
2. 代码
<select id="selectItem" class="form-control">
<option>北京</option>
<option>上海</option>
<option>广州</option>
<option>深圳</option>
</select>
$("#selectItem").focus(function() {
// 动态设置size支持option的事件监听
this.size = this.children.length;
}).blur(function() {
this.size = 1;
});
$("#selectItem option").click(function() {
$("#selectItem option").each(function() {
$(this).removeAttr("selected");
});
$(this).attr("selected", true);
$("#selectItem").blur();
});
$(document).keydown(function(event) {
var key = event.which; // 获取按键的ascii码
var num = key - 48; // 获取对应的数字
if (num >= 1 && num <= 4) { // 只有4个下拉框,只处理1-4
var count = 1; // 计数
$("#selectItem option").each(function() {
if (count == num) {
$("#selectItem").val($(this).text());
$(this).attr("selected", true);
$("#selectItem").blur();
} else {
$(this).removeAttr("selected");
}
count++;
});
}
});
当select是动态生成的时候,绑定事件不生效,可以使用事件冒泡实现事件绑定
$("body").delegate("#selectItem", "focus", function() {
this.size = this.children.length;
}).delegate("#selectItem", "blur", function() {
this.size = 1;
}).delegate("#selectItem option", "click", function() {
$("#selectItem option").each(function() {
$(this).removeAttr("selected");
});
$(this).attr("selected", true);
$("#selectItem").blur();
});
$(document).keydown(function(event) {
var key = event.which; // 获取按键的ascii码
var num = key - 48; // 获取对应的数字
if (num >= 1 && num <= 4) { // 只有4个下拉框,只处理1-4
var count = 1; // 计数
$("#selectItem option").each(function() {
if (count == num) {
$("#selectItem").val($(this).text());
$(this).attr("selected", true);
$("#selectItem").blur();
} else {
$(this).removeAttr("selected");
}
count++;
});
}
});
为select的option绑定键盘事件的更多相关文章
- html5标签div可绑定键盘事件方法
我们知道键盘事件一般用在input这些可以获取焦点的元素上,但是作为div,尽管官方上说可以绑定键盘事件,但是我写了以后发现不生效,于是找答案. 果然,找到了一个神器:tabindex 它是html5 ...
- select中option的onclick事件失效
html: <select id="pageSelect"> <option value="1" selected onclick=" ...
- easyui给select控件绑定change事件
一般的做法是使用jQuery来绑定,例如: $("#id").change(function(){ alert("change事件绑定"); }); 当给sel ...
- 让 select 的 option 标签支持事件监听(如复制操作)
这标题,让option支持事件监听,应该不难的呀,有什么好讲的? 其实还是有的,默认在浏览器代码是无法直接对option标签进行操作的,不仅包括JS事件监听,还是CSS样式设置 查了一些资料,姑且认为 ...
- layui 触发 select 下option 被选择事件
1.找到值为某一个的元素 var S0 = 'dd[lay-value='+ level+']'; 2.给该元素注册点击事件 siblings(兄弟节点) $('select[name=\'leve ...
- js 绑定的键盘事件
在全局绑定键盘事件 document.onkeydown = function(event){ //在全局中绑定按下事件 var e = event || window.e; va ...
- javascript 键盘事件总结
原文:http://www.cnblogs.com/rubylouvre/archive/2009/08/20/1550526.html#2583814 在进入正题前,我们看一下浏览器对于键盘的一些默 ...
- vue添加页面键盘事件
我司开发项目,用的是vue+elementUI,做登陆页面的时候,点击enter键的时候要实现和点击登陆按钮一样的功能,所以就百度了一下,于是一通百度之后,就在点击按钮上面直接添加了@keyup.en ...
- js Dom为页面中的元素绑定键盘或鼠标事件
html鼠标事件 onload 页面加载 onclick 鼠标单击 onmouseover 鼠标移入 onmouseout 鼠标移出 onfocus 获取焦点 onblur 失去焦点 onchange ...
随机推荐
- NServiceBus入门:启程(Introduction to NServiceBus: Getting started)
原文地址:https://docs.particular.net/tutorials/intro-to-nservicebus/1-getting-started/ 侵删. 最好的学习NService ...
- FIREMONEY手机虚拟键盘遮挡的解决
FIREMONEY手机虚拟键盘遮挡的解决 尝遍了网上人们提供的N种方法之后,发现还是老猫的方法才是彻底解决问题的办法. 老猫“不看后悔XXX”--->RAD10.2.3 Flying Wang ...
- ImageWriter制作ubuntu的U盘启动盘
转自:http://my.oschina.net/f839903061/blog/197935?p={{currentPage+1}} 1.工具从ubuntn中文网中下载指定软件:ImageWrite ...
- Google Breakpad 完全解析(二) —— Windows前台实现篇
原创文章,转载请标明出处:Soul Apogee (http://bigasp.com),谢谢. 好,看完了如何使用breakpad,我们现在看看breakpad在Windows下到底是如何实现的呢? ...
- Android进阶笔记:AIDL内部实现详解 (二)
接着上一篇分析的aidl的流程解析.知道了aidl主要就是利用Ibinder来实现跨进程通信的.既然是通过对Binder各种方法的封装,那也可以不使用aidl自己通过Binder来实现跨进程通讯.那么 ...
- PTC介绍
付费点击或按就付(英文:Paid-To-Click,缩写作PTC或pay_per-click)是一种点击付费的线上商业模式.PTC的经营模式是以PTC网站作为广告客户和消费者的仲介,广告客户付钱给经营 ...
- 【LaTeX】E喵的LaTeX新手入门教程(6)中文
假期玩得有点凶 ._.前情回顾[LaTeX]E喵的LaTeX新手入门教程(1)准备篇 [LaTeX]E喵的LaTeX新手入门教程(2)基础排版 [LaTeX]E喵的LaTeX新手入门教程(3)数学公式 ...
- [转]Working with Transactions (EF6 Onwards)
本文转自:http://www.cnblogs.com/xiepeixing/p/5275999.html Working with Transactions (EF6 Onwards) This d ...
- iOS:导航控制器侧滑出栈实现
介绍:在iOS中,导航控制器UINavigationController是默认实现左侧边缘侧滑手势出栈的,但是如果当开发者对导航控制器子控制实现自定义leftBaButtonItem时,这个侧滑功能就 ...
- iOS: 使用故事板和xib设置按钮圆角方法
使用storyboard如何设置圆角或边框? 通过storyboard的 运行时属性runtime attribute,可以对Button设置圆角或者边框 1.很多人都知道,通常设置一个 Button ...