javascript 在html输入文本框中按回车键时如何防止回发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21579724/
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
How to prevent postback while pressing enter key from inside html input textbox?
提问by Karthi Keyan
Am using html input textbox in .aspx page, when i change values in that textbox and hit enter key means page get postback. How to prevent this postback other than returning false in "onkeypress" event.
我在 .aspx 页面中使用 html 输入文本框,当我更改该文本框中的值并按 Enter 键时,意味着页面得到回发。除了在“onkeypress”事件中返回 false 之外,如何防止这种回发。
Code:
代码:
textBox[0].setAttribute("onkeydown", "return (event.keyCode!=13);");
Thanks,
Karthik.
谢谢,
卡西克。
回答by Mister Epic
You need to use javascript to prevent the default behaviour of clicking enter, which is to submit the form. You need to do something like this (and I'm using jQuery here, which is included by default with an ASP.Net app):
您需要使用javascript来防止点击回车的默认行为,即提交表单。你需要做这样的事情(我在这里使用 jQuery,它默认包含在一个 ASP.Net 应用程序中):
$('input[type="text"]').keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});