jQuery 禁用输入提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16062525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable enter submit
提问by user2288298
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 Enterwhile the textfield is selected.
我有一个带有文本字段的表单,如果用户Enter在选择文本字段时按下,我试图禁用浏览器提交整个表单时的默认行为。
$('#recaptcha_response_field').keydown(function(event) { if (event.keyCode == 13) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
alert("You Press ENTER key");
return false;
}
});
Currently am getting "You Press ENTER key" and the default behavior isn't overridden.
目前正在收到“您按 ENTER 键”并且默认行为未被覆盖。
回答by pala?н
回答by dustysilicon
I found this and it works for me.
我找到了这个,它对我有用。
<input type="text" name="somename" id="someid" value="" onkeypress="return event.keyCode!=13">
回答by Rob Powell
To prevent the script from blocking the enter key on other elements such as on a textarea. Change the target from "form" to "input".
防止脚本阻止其他元素(例如文本区域)上的 Enter 键。将目标从“表单”更改为“输入”。
$(document).on("keypress", "input", function (e) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
return false;
}
});
回答by prabeen giri
If none of the above solution is working for you , and you are using javascript anyway , why don't you use button type as 'button' <input type='button'>
and submit it using javascript
如果上述解决方案都不适用于您,并且您仍然使用 javascript,为什么不使用按钮类型作为“按钮”<input type='button'>
并使用 javascript 提交它
$('#form_id').submit();
回答by Grey Wolf
U can use this script to prevent enter on entire from.
你可以使用这个脚本来防止整个输入。
<script type="text/javascript">
$(document).on("keypress", 'form', function (e) {
if (e.target.className.indexOf("allowEnter") == -1) {
var code = e.keyCode || e.which;
if (code == 13) {
e.preventDefault();
return false;
}
}
});
</script>
you put the classname to which control need to use enter
你把需要使用的控件的类名输入