简体   繁体   中英

When I press enter key, unable to login

I have been trying to log in to a offline HTML/CSS page using the enter key but unable to start with JavaScript which needs I'm sure.

But can log in using the mouse when I click the log in button which i have created ..

How do i use the enter key stroke to log in?

This is the javascript which I have hard coded for credential test which works using the mouse click.. I want it for the enter key.. Thank you.

<!DOCTYPE HTML>
<html>
<head>
<title>
Login page
</title>
</head>
<body>
<div align="center">
<div id="bor">

<h1>
   Login Page
</h1>

<div>
<form name="login">
&nbsp<input type="text" placeholder="Enter the Username" name="user"/> <br />
 <input type="password" placeholder="Enter the Password" name="pwd"/><br /><br />
 <input type="checkbox" name="en" value="en" />Remember Me<br>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Cancel"/>
</form>
</div>


<script language="javascript">
function check(form)
{
   if(form.user.value == "user" && form.pwd.value == "pwd")
   {
       window.open('inbox.html','_self')
   }
   else
   {
       alert("Error Password or Username")
   }
}
</script>

Use the submit event handler. The click handler on an element will only fire if you click on it. What you are trying to do is submitting a form, but handling the form with javascript instead of on the server. I would recommend binding that dynamically, but as you have all javascript here inline, I'll give an example inline too.

You'll need to attach a submit event handler to the form element. If you do it inline, this will work with onsubmit="..." instead. The return is there to prevent/allow the form to be submitted to the server. If the function returns 'true', the form will be submitted to the server. If the function returns 'false', the form will not be submitted. You'll also need to change the type of your submit button to submit . This will tell the browser to submit the form if that button is clicked. Alternatively, if you press enter in an input field, the browser will see this as submitting the form too.

<form name="login" onsubmit="return check(this)">
&nbsp<input type="text" placeholder="Enter the Username" name="user"/> <br />
  <input type="password" placeholder="Enter the Password" name="pwd"/><br/><br/>
  <input type="checkbox" name="en" value="en" />Remember Me<br>
  <input type="submit" value="Login"/>
  <input type="reset" value="Cancel"/>
</form>

The javascript behind it will remain mostly the same. You'll notice that we passed this to the function. This will pass the element itself (in this case the form element) to the function. As said before, you'll need to return false . I've changed form to frm as form is a globally defined variable in some browsers.

function check(frm)
{
   if(frm.user.value == "user" && frm.pwd.value == "pwd")
   {
       window.open('inbox.html','_self')
   }
   else
   {
       alert("Error Password or Username")
   }

   return false;
}

An example jsfiddle: http://jsfiddle.net/AS5t5/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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