简体   繁体   English

我如何使用回车键提交用户名和密码

[英]how do i use an an enter key stroke to submit a username and password

I have a login button that works fine,it logs a user in etc.. but i want to allow the user to press the enter key to login as well.我有一个工作正常的登录按钮,它可以让用户登录等。但我也想让用户按 Enter 键登录。 how do i do this.I tried a test using onkeypress but it didnt do anything as bellow我该怎么做。我尝试使用 onkeypress 进行测试,但没有执行以下任何操作

<form>
      <div class="form-group">
        <input type="text" class="form-control" placeholder="Username id="username" />
       </div>
      <div class="form-group">
      <input type="password" class="form-control" placeholder="........" id="password" onkeypress=myFunction() /> //i just tried this myFunction() to see if it would give an alert but it doesnt
      </div>
     <div class="form-group">
   <button type="submit" class="btn btn-primary btn-login" id="btnLogin">Log in</button>

  </div>
  </div>
  </form>

function myFunction()
 { alert("button pressed")}

so how do i use the enter key to submit my request in javascript and jquery那么我如何使用回车键在 javascript 和 jquery 中提交我的请求

As you've placed the input within a form element which contains a submit button, you get this behaviour by default.当您将input放置在包含提交按钮的form元素中时,默认情况下您会获得此行为。 To hook to it, use the submit event of the form, like this:要挂钩它,请使用表单的submit事件,如下所示:

 $('form').on('submit', function(e) { e.preventDefault(); // only used to stop the form submission in this example console.log('form submitted'); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <div class="form-group"> <input type="text" class="form-control" placeholder="Username" id=" username" /> </div> <div class="form-group "> <input type="password" class="form-control" placeholder="........" id="password" /> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-login" id="btnLogin">Log in</button> </div> </form>

Note that I fixed the missing " after the placeholder attribute in the first input . You also don't need the trailing space after all the attribute values, so I removed those too.请注意,我修复了第一个input placeholder属性之后缺少的" 。在所有属性值之后您也不需要尾随空格,因此我也删除了它们。

First you need to send the Event to the myFunction() function and add ID to your form to add submit manually::首先,您需要将事件发送到myFunction()函数并将 ID 添加到您的表单以手动添加提交:

HTML HTML

<form id='myForm'>
<!-- form actions (etc..) -->
<input type="password" class="form-control" placeholder="........" id="password" onkeypress=myFunction(e) />
<!-- form actions (etc..) -->
</form>

Now in ASCII the reenter code is 13 all you need to check is when the pressed key is reenter (13) then you need to take the event key code and call the submit function::现在在 ASCII 中,重新输入代码是13您需要检查的是当按下的键重新输入时(13),然后您需要获取事件键代码并调用提交函数:

function myFunction(e)
var code = (e.keyCode ? e.keyCode : e.which);
{
   if (code == "13")
    {
        //Calling the submit or clicking manually it 
        $( "#myForm" ).submit();   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM