禁用输入提交 [英] Disable enter submit

查看:91
本文介绍了禁用输入提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我内部有一个带有textfield的表单,如果用户在选择文本字段时按 Enter ,我试图在浏览器提交整个表单时禁用默认行为。

I have a form with a textfield inside and I am trying to disable the default behavior when the browser submits the whole form if the user presses Enter while the textfield is selected.

$('#recaptcha_response_field').keydown(function(event) { if (event.keyCode == 13) {
     event.preventDefault();
     event.stopPropagation();
     event.stopImmediatePropagation();
     alert("You Press ENTER key");
     return false;
   } 
});

目前正在获取您按ENTER键,并且未覆盖默认行为。

Currently am getting "You Press ENTER key" and the default behavior isn't overridden.

推荐答案

试试这个:

Try this:

$(document).on("keypress", 'form', function (e) {
    var code = e.keyCode || e.which;
    if (code == 13) {
        e.preventDefault();
        return false;
    }
});

这会阻止 keypress

在此试用

这篇关于禁用输入提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆