java的文本框如何回车键触发按钮_如何获得一个文本框内回车键触发功能,而不是第一/默认按钮...

该博客讨论了如何在JavaScript中处理特定文本框内的Enter键按下事件,而不是触发默认的按钮点击事件。作者提供了一个解决方案,通过监听文本框的keypress事件,并在事件处理函数中检查keyCode是否为13(Enter键),如果是,则弹出提示并阻止按钮的默认点击行为。
摘要由CSDN通过智能技术生成

I am trying to get the Enter key to trigger a function when it is pressed when inside a certain textbox, and not trigger the first or default button.

You can see an example of what is happening here: http://jsfiddle.net/cutsomeat/WZ6TM/1/

If you press other keys you will get an alert box with the keycode, yet if you press the Enter key you will not get the alert box with the keycode, but rather the alert box within the button click event.

Obviously the Enter key is trigger the button. Is there a way to avoid this and instead, capture the Enter key in the keyup event, then trigger another function?

解决方案

Try this:

$('#myText').live("keypress", function(e) {

if (e.keyCode == 13) {

alert("Enter pressed");

return false; // prevent the button click from happening

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值