javascript jQuery('#class').keypress Event.keyCode == 13) 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20362538/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:18:44  来源:igfitidea点击:

jQuery('#class').keypress Event.keyCode == 13) Not working

javascriptjquery

提问by Matt The Ninja

Ok so i have a form with a div submit and want certain inputs on 'enter key' press to submit form. my html

好的,所以我有一个带有 div 提交的表单,并希望按“输入键”进行某些输入以提交表单。我的 HTML

<li>
    <label> <span>Username</span>

        <input placeholder="Please enter your username" name="username" class="login_username loginforminput" pattern="[a-zA-Z0-9]{6,}" type="text" tabindex="1" required autofocus>
    </label>
</li>
<li>
    <label> <span>Password</span>

        <input placeholder="Please enter your password" name="password" class="login_password loginforminput" pattern=".{6,}" type="password" tabindex="2" required>
    </label>
</li>
<li>
    <div name="login-submit" id="login-submit" tabindex="3">Sign In</div>
</li>
<li>
    <div class="gotootherform" id="registerpopup" tabindex="4">Register</div>
</li>

And my .js script.

还有我的 .js 脚本。

////Clicking 'Enter' on Register Form
  jQuery('#loginforminput').keydown(function(event) {
    if (Event.keyCode == 13) {
            RegisterFormSubmit();   
    }
});

the function works as assigned it in onclick for a div so isnt that. What am i doing wrong?

该函数的工作原理是在 onclick 中为 div 分配的,所以不是。我究竟做错了什么?

回答by Matt Zeunert

Javascript is case sensitive, so you need to change this

Javascript 区分大小写,因此您需要更改此设置

if (Event.keyCode == 13) {
        RegisterFormSubmit();   
}

to this

对此

if (event.keyCode == 13) {
        RegisterFormSubmit();   
}

Also # selects by id rather than class, use '.' to match classes:

另外 # 按 id 而不是类选择,使用 '.' 匹配类:

 jQuery('.loginforminput')

回答by Ben Schmidt

Also you should be using:

你也应该使用:

jQuery('.loginforminput')instead of jQuery('#loginforminput')

jQuery('.loginforminput')代替 jQuery('#loginforminput')

The # is used for id="" while the . is used for classes

# 用于 id="" 而 . 用于类

回答by Eddie Monge Jr

You should be assigning the handler to the <form>tag. If there isn't one, create one. Also, hitting enter usually submits a form anyway.

您应该将处理程序分配给<form>标签。如果没有,就创建一个。此外,无论如何,按回车键通常都会提交表单。

$('form').on('submit', function(e) {
  e.preventDefault();
  RegisterFormSubmit();
});