javascript HTML 表单 - 按“Enter”时执行脚本,而不提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12048796/
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
HTML Form - Execute script when pressing 'Enter', without submitting
提问by Dr.Kameleon
Possible Duplicate:
jQuery Event Keypress: Which key was pressed?
可能的重复:
jQuery 事件按键:按下了哪个键?
OK, here's my situation (I've solved this issue in the past, but since I'm not sure my approach was the best, I'm all ears for some advice):
好的,这是我的情况(我过去已经解决了这个问题,但由于我不确定我的方法是最好的,所以我非常愿意听取一些建议):
- I've got a simple HTML form
- The purpose of this form is NOT to be submitted EVER
- The user normal clicks the (pseudo)
Submit
button, and the desired action is executed (via Javascript/Ajax, etc)
- 我有一个简单的 HTML 表单
- 永远不要提交此表格的目的
- 用户正常单击(伪)
Submit
按钮,并执行所需的操作(通过 Javascript/Ajax 等)
The thing is :
事情是 :
- If the user hits enter, then the form issubmitted, something we definitely DON'T WANT.
- What we want is to catchthat particular keypress, and in that case, trigger the action that would normally be executed if the button was clicked.
- 如果用户点击进入,然后形式被提交,这是我们绝对不希望。
- 我们想要的是捕捉那个特定的按键,在这种情况下,触发通常会在按钮被点击时执行的动作。
How would you go about this?
你会怎么做?
My Approach
我的方法
The Form
表格
<form id="someId" action="#" method="post"
onsubmit="return false;" onkeypress="return enterSub(this,event);">
...
</form>
The Code
代码
function enterSub(inField, e)
{
var charCode;
if(e && e.which)
{
charCode = e.which;
}
else if (window.event)
{
e = window.event;
charCode = e.keyCode;
}
if (charCode == 13)
{
performThatAction; // My Main action
}
}
回答by Kevin B
Bind to the submit of the form rather than the click of a button, then prevent the default action.
绑定到表单的提交而不是单击按钮,然后阻止默认操作。
$("#myform").submit(function(e){
e.preventDefault();
alert("The action has occurred without submitting the form!");
});
回答by orhanhenrik
$('#form_id').submit(function(){
//do something
});
This function will trigger whenever user tries to submit the form (you will have to return false or prevent default)
每当用户尝试提交表单时都会触发此功能(您必须返回 false 或防止默认)
回答by NewInTheBusiness
If you also would like to do something specific when users hit Enter, you could do something like:
如果您还想在用户按 Enter 时执行某些特定操作,您可以执行以下操作:
$(document).keypress(function(e) {
if(e.which == 13) {
e.preventDefault();
// Do something
}
});
回答by sasikals26
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
document.attachEvent("onkeydown", win_onkeydown_handler);
function win_onkeydown_handler()
{
alert(event.keyCode);
switch (event.keyCode){
case 13 : // 'enter'
event.returnValue = false;
event.keyCode = 0;
alert("Enter Blocked");// If need show this alert else comment it
break;
}
}
</script>
<BODY>
</BODY>
</HTML>
Please use the above script to find enter key press u can handle the enter as u needed.Hope ur doubt as solved else please post me.
请使用上面的脚本找到回车键,您可以根据需要处理回车。希望您的疑问已解决,否则请寄给我。