button绑定回车事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 很多时候,我们为一个表单中的button写了事件,但它不是submit,不能实现按回车键提交表单,那么就要为这个添加绑定事件了。 html代码片段如下: <tr> <td><input type= "text" name= "title" ></td> <td><input type= "button" name= "button" class = "but" id= "but" ></td> </tr> JS示例: function BindEnter() { if ( event .keyCode == 13) { event .cancelBubble = true ; event .returnValue = false ; document.getElementById( 'but' ).click(); } } 咱们可以把BindEnter() 事件绑定在input上,上面的html代码第2行改写成: <td><input type= "text" name= "title" onkeypress= "BindEnter();" ></td> jQuery示例: $( ".but" ).click(function(){ //具体功能代码略 }) $( "input[type='text']" ).keypress(function(e){ if ( event .keyCode == 13) { event .cancelBubble = true ; event .returnValue = false ; $( this ).parents( "tr" ).find( ".but" ).click(); } }) 使用 class 来标识按钮,这样具有更强的兼容性,比如有很多行类似的<tr>的数据时,每行一个按钮。 |
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签