禁用输入提交

问题描述:

我内部有一个带有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

在此试用