jQuery 禁用特定文本框上的输入键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5482825/
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 key on specific textboxes
提问by alex
I was looking for a way to do this, got a few scripts, but none of them is working for me.
我正在寻找一种方法来做到这一点,得到了一些脚本,但没有一个对我有用。
When I hit enter in my textboxes, it shouldn't do anything.
当我在文本框中按 Enter 键时,它不应该执行任何操作。
I have tried a few JavaScript and jQuery scripts, but nothing is working for me.
我尝试了一些 JavaScript 和 jQuery 脚本,但没有任何效果。
$(document).ready(function() {
$('#comment').keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
}
});
$('#comment').keyup(function() {
var txt = $('#comment').val();
$('#comment').val(txt.replace(/[\n\r]+/g, " "));
});
});
回答by alex
This works for me.
这对我有用。
$('textarea').keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
}
});
Your second piece looks like it is designed to capture people pasting in multiple lines. In that case, you should bind it to keyup paste
.
你的第二个作品看起来像是为了捕捉多行粘贴的人。在这种情况下,您应该将其绑定到keyup paste
.
回答by Akshay
If you want to prevent Enter key for specific textbox then use inline js.
如果要防止特定文本框的 Enter 键,请使用内联 js。
<input type="text" onkeydown="return (event.keyCode!=13);"/>
回答by PHP Ferrari
It stops user to press Enterin Textbox & here I return falsewhich prevent form to submit.
它阻止用户在文本框中按Enter,在这里我返回false以防止表单提交。
$(document).ready(function()
{
// Stop user to press enter in textbox
$("input:text").keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
回答by polarblau
Works for me:
对我有用:
$('#comment').keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
}
});
UPDATE
更新
In case you're trying to prevent the submitting of the parent form rather than a line break (which only makes sense, if you're using a textarea
, which you apparently aren't doing?) you might want to check this question: Disable the enter key on a jquery-powered form
如果您试图阻止提交父表单而不是换行符(这才有意义,如果您使用的是textarea
,而您显然没有这样做?)您可能想要检查这个问题:禁用jquery 驱动的表单上的回车键
回答by user1853583
I found this combine solution at http://www.askmkhize.me/how-can-i-disable-the-enter-key-on-my-textarea.html
我在http://www.askmkhize.me/how-can-i-disable-the-enter-key-on-my-textarea.html找到了这个组合解决方案
$('textarea').keypress(function(event) {
if ((event.keyCode || event.which) == 13) {
event.preventDefault();
return false;
}
});
$('textarea').keyup(function() {
var keyed = $(this).val().replace(/\n/g, '<br/>');
$(this).html(keyed);
});
回答by Nikunj Dhimar
If you want to disable for all kind of search textbox. Give all them a class and use that class like 'search_box' following.
如果您想禁用所有类型的搜索文本框。给所有他们一个类并使用该类,如下面的“search_box”。
//prevent submission of forms when pressing Enter key in a text input
$(document).on('keypress', '.inner_search_box', function (e) {
if (e.which == 13) e.preventDefault();
});
cheers! :)
干杯!:)
回答by A.Khan
I don't understand why everybody here is suggesting to use jquery. There is a very simple method for it for a specific input element field. You can use its onKeyDown attribute and use e.preventDefault() method in it. Like this:
我不明白为什么这里的每个人都建议使用 jquery。对于特定的输入元素字段,有一个非常简单的方法。您可以使用其 onKeyDown 属性并在其中使用 e.preventDefault() 方法。像这样:
<input type="text" onKeyDown={(e) => e.preventDefault()}/>
Note: This is React JSX way of using this attribute you can use HTML basic way.
注意:这是使用此属性的 React JSX 方式,您可以使用 HTML 基本方式。
回答by Marc Uberstein
Use global selector $('input')...if you use multiple text boxes on page the ID ('#comment') will cause problems. And when using dynamic content bind it using .live e.g.
使用全局选择器 $('input')...如果在页面上使用多个文本框,ID ('#comment') 将导致问题。当使用动态内容时,使用 .live 绑定它,例如
$('input:not(textarea)').live('keypress',function(e) {
if (e.which == 13) return false;
if (e.which == 13) e.preventDefault();
});
(Disable enter for text boxes and leaves enter enabled for text areas)
(禁用文本框的输入,并为文本区域启用输入)
回答by ariel
<script type="text/javascript">
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
</script>