我如何触发 按回车键?

发布于 2024-10-22 05:53:24 字数 2132 浏览 1 评论 0原文

所以,我需要修复一个错误:每当你按下“enter”键时;表单不提交。基本上原因是因为触发表单提交的元素是一个 href...我尝试将其设置为提交,但随后 css 失败,所以我想知道是否有办法使其使用 Enter 键提交?

这是代码:

        <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>&nbsp;</td>
                    <td>
                    <fb:login-button perms="email" id="facebook-login-button"></fb:login-button>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="checkbox"><label for="request_password">{translate id='Game'}<a href="forgotPassword.php">Request password</a>{/translate}</label></td>
                </tr>
                <tr>
                    <td>&nbsp;</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>

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?

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

望笑 2024-10-29 05:53:24

实际上,我找到了一种方法来修复它......我所要做的就是使用 display:none 和visible:false; 进行提交;并且没有任何价值让输入工作......我知道这很奇怪,但我来这里是为了修复错误而不是重新设计代码:(

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 :(

我的奇迹 2024-10-29 05:53:24

这是 jQuery 的答案。但你明白了。

这将检测从表单中的任何输入中按下的 Enter 键,并将触发表单的 submit() 方法

$("#login_form input").keypress(function(e) {
  if (e.keyCode == 13) {
    $(this).closest("form").submit();
  }
});

Here's a jQuery answer. But you get the idea.

This will detect the enter key being pressed from within any input in the form and it will trigger the form's submit() method

$("#login_form input").keypress(function(e) {
  if (e.keyCode == 13) {
    $(this).closest("form").submit();
  }
});
三生池水覆流年 2024-10-29 05:53:24

您是否尝试过文本框的 onkeyup:

if (event.keyCode == 13) { document.getElementById("connect").click(); }

我认为应该有效。还没有测试过。

Have you tried on the onkeyup for the textbox:

if (event.keyCode == 13) { document.getElementById("connect").click(); }

I think that should work. Haven't tested it.

佞臣 2024-10-29 05:53:24

尽管正确的方法是使其成为提交按钮并相应地更改 CSS(您可以使提交按钮看起来像链接),但您可以注册一个 javascript 事件来更改 document.location 或使用 form.submit() 提交表单

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()

心如荒岛 2024-10-29 05:53:24

好吧,我想我通过两种方式帮助您

1- 当焦点位于 href 时您需要

2- 当焦点位于文档上时您需要 一切都好

,如果您需要 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;">

并且它位于文档上当你按下回车键时,请点击 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;">

抱歉,我是第一篇文章,我是新来的
我希望我能帮忙:)

well i think i got you in 2 ways

1- you need when the focus is on the href

2- you need when the focus is on the document all

well if you need it on the focus of the 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

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文