javascript 如何使用回车键触发 <a href> ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5315678/
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 can I fire an <a href> with enter key press?
提问by Ave
So, I need to fix a bug: Whenever you hit "enter" key; the form doesn't submit. Basically the cause is because the element that trigger form submit is an href... I tried making it a submit but then the css fails so I was wondering if there is a way to make it submit with enter key?
所以,我需要修复一个错误:每当你按下“回车”键;表单不提交。基本上原因是因为触发表单提交的元素是一个href...我尝试将其提交但随后css失败了所以我想知道是否有办法使用输入键提交它?
Here is the code:
这是代码:
<div class="login-form-bg">
<form name="login_form" id="login_form" method="post">
<table>
<tr>
<td>{translate id='Game'}Login:{/translate}</td>
<td><input type="text" name="ss_users_pseudo" /></td>
</tr>
<tr>
<td>{translate id='Game'}Password:{/translate}</td>
<td><input type="password" name="ss_users_password" /></td>
</tr>
<tr height="30px">
<td> </td>
<td>
<fb:login-button perms="email" id="facebook-login-button"></fb:login-button>
</td>
</tr>
<tr>
<td> </td>
<td class="checkbox"><label for="request_password">{translate id='Game'}<a href="forgotPassword.php">Request password</a>{/translate}</label></td>
</tr>
<tr>
<td> </td>
<td class="checkbox">{translate id='Game'}Remember me{/translate} <input type="checkbox" name="ss_users_remenber_me" value="1" /></td>
</tr>
</table>
<a href="#" onclick="document.login_form.submit(); return false;" name="connect" id="connect">
<div class="button-action login-button-div">
<span><span><span>
{translate id='Game'}Login{/translate}
</span></span></span>
</div>
</a>
</form>
回答by Ave
Actually, I found a way to fix it... all I had to do is make a submit with display:none and visible:false; and no value to make the enter work.... I know it's weird, but I'm here to fix bugs not redesign the code :(
实际上,我找到了解决它的方法......我所要做的就是使用 display:none 和 visible:false; 进行提交。并且没有任何价值使输入工作......我知道这很奇怪,但我来这里是为了修复错误而不是重新设计代码:(
回答by jessegavin
Here's a jQuery answer. But you get the idea.
这是一个 jQuery 答案。但是你明白了。
This will detect the enter key being pressed from within any input in the form and it will trigger the form's submit()
method
这将检测从表单中的任何输入中按下的回车键,并将触发表单的submit()
方法
$("#login_form input").keypress(function(e) {
if (e.keyCode == 13) {
$(this).closest("form").submit();
}
});
回答by AbiusX
Tough the right approach would be to make it a submit button and change CSS accordingly (you can make a submit button look like a link), You could register a javascript event that changes document.location or submits a form with form.submit()
正确的方法是将其设置为提交按钮并相应地更改 CSS(您可以使提交按钮看起来像一个链接),您可以注册一个 javascript 事件来更改 document.location 或使用 form.submit() 提交表单
回答by SAGExSDX
Have you tried on the onkeyup for the textbox:
您是否尝试过文本框的 onkeyup:
if (event.keyCode == 13) { document.getElementById("connect").click(); }
I think that should work. Haven't tested it.
我认为这应该有效。没测试过
回答by Marwan
well i think i got you in 2 ways
好吧,我想我通过两种方式得到了你
1- you need when the focus is on the href
1- 当焦点在 href 上时您需要
2- you need when the focus is on the document all
2- 当焦点在文档上时,您需要全部
well if you need it on the focus of the a href
好吧,如果你需要它在 a href 的焦点上
function hrefKeyDown(evt)
{
var ev = ((window.event)?(window.event):(evt));
var ahref = ((window.event)?(window.event.srcElement):(evt.currentTarget));
var charCode = ((window.event)?(window.event.keyCode):(((evt.which)?(evt.which):(evt.keyKode))));
if(charCode==13)
{
ahref.click();
}
}
<a href="#" onclick="document.login_form.submit(); return false;" name="connect" id="connect" onkeydown = "hrefKeyDown(event);return false;">
and if its on the document when ever you press enter click the a href
如果它在文档上,当你按下回车键时点击 a href
function hrefKeyDown(evt)
{
var ev = ((window.event)?(window.event):(evt));
var ahref = document.getElementById('connect');
var charCode = ((window.event)?(window.event.keyCode):(((evt.which)?(evt.which):(evt.keyKode))));
if(charCode==13)
{
ahref.click();
}
}
<body onkeydown="hrefKeyDown(event);return false;">
sorry for the first post iam new here i hope i can help :)
对不起,我是新来的第一篇文章,希望我能帮上忙:)