Javascript: Enter does not work to send a password.

I cannot get the following code: when I press the enter key in the text box, the function is not called. I do not understand why, though ...

<form>
<p align="center">
<input type="password" class="password" name="text1" onkeypress="submitonenter(text1.value,"money","cash",event)" /><br>
<input type="button" value="Enter" style="width: 100px" name="Enter" onclick=javascript:validate(text1.value,"money","cash") />
</p>
</form>

<script type="text/javascript">
    function submitonenter(text1,text2,text3,evt) {
        evt = (evt) ? evt : ((window.event) ? window.event : "")
        if (evt) {
            // process event here
            if ( evt.keyCode==13 || evt.which==13 ) {

 if (text1==text2)
 load('money/welcome.html');
 else 
 {
  if (text1==text3)
  load('cash/welcome.html');
   else
   {
   load('failure.html');
   }
 }

            }
        }
    }
</script>

<script language = "javascript">
function validate(text1,text2,text3)
{
 if (text1==text2)
 load('money/welcome.html');
 else 
 {
  if (text1==text3)
  load('cash/welcome.html');
   else
   {
   load('failure.html');
   }
 }
}
function load(url)
{
 location.href=url;
}
</script>
+3
source share
4 answers

I'm not sure why you need the submitOnEnter function at all.

Why not just change the input type='button'to type='submit'and change the keyword onclickto onsubmit?

EDIT: An apology, of course, 'onsubmit'should be put in the form tags, not the input. Providing the following:

<form onsubmit=validate(text1.value,"money","cash") >
  <p align="center">
    <input type="password" class="password" name="text1" /><br>
    <input type="submit" value="Enter" style="width: 100px" name="Enter" />
  </p>
</form>
+2
source

input type="submit" ( , Firefox):

<form id="myForm" method="POST" action="failure.html" onsubmit="return validate(document.getElementById('text1').value,'money','cash');">
<p align="center">
<input type="password" class="password" name="text1" id="text1"/><br>
<input type="submit" value="Enter" style="width: 100px" name="Enter" />
</p>
</form>

<script language = "javascript">
    function validate(text1,text2,text3) {
         var form=document.getElementById('myForm');
         if (text1==text2)
            form.action='money/welcome.html';
         else {
          if (text1==text3)
            form.action='cash/welcome.html';
           else {
              form.action='failure.html';
           }
         }
         return true;
    }
</script>

: onSubmit, @mway ().

+1

, - onclick, , onsubmit , / .

0

, , , "evt.preventDefault()", (.. ). , , location.href, , , .

, , , . . javascript ( ), . , . , , .:) , (, ). , , .

0

Source: https://habr.com/ru/post/1767182/


All Articles