简体   繁体   中英

Combine onclick and onkeypress

I have this html form, which does not actually sends data to a php file, buts sends data to a javascipt function.

Works fine if I click the button, but when I hit enter nothing happens.

I would like the button to work if clicked on with the mouse or the user hits enter.

How do I fix this? Thanks

       <input type="button" value="Reveal Now" style="margin: 3px;font-family: 'festivo_lcbasic', Arial, Helvetica, sans-serif; text-transform:uppercase; font-size:26px; text-decoration:none; font-weight:normal; padding-top: 10px;"  onclick="check(this.form.Pass.value)"  onkeypress="check(this.form.Pass.value)"/>
  </form>

  <script type="text/javascript">

  var password = ["#123, #345"];

  function check(pass) {
    for(a = 0; a < password.length; a++) {
      if (pass == password[a]) {
        top.location.href="enter.html";
        return;
      }
    }
    location.href="incorrect.html";
  }

  </script>

I think you are trying to check something when user enters enter-key when typing in text field. I am assuming your input text is like

<input type="text" id="Pass" />

to

<input type="text" id="Pass"  onkeypress="runScript(event)" />

and add this function too in your script

function runScript(e) {
    if (e.keyCode == 13) {
        var pas = document.getElementById("Pass").value;
        check(pas) //calling your button function

    }
}

Complete script:

<script type="text/javascript">

var password = ["#123, #345"];

function check(pass) {
for(a = 0; a < password.length; a++) {
    if (pass == password[a]) {
       top.location.href="enter.html";
        return;
    }
  }
  location.href="incorrect.html";
}

function runScript(e) {
    if (e.keyCode == 13) {
        var pas = document.getElementById("Pass").value;
        check(pas) //calling your button function

    }
}

</script>

OR:

<input type="text" id="Pass"  onkeypress="runScript(event, this.value)" />

script:

function runScript(e, pas) {
    if (e.keyCode == 13) {
        check(pas) //calling your button function

    }
}

Your code is terrible for many reasons that I don't get into now, but if you look in your javascript console (chrome/firefox) you'll see this error:

Here's how i fixed your code. 1. Added ID 'myButton' to your 2. Passed 'myButton' to your check() function 3. Used getElementById() to get to your from the function

<input id="myButton" type="button" value="Reveal Now" style="margin: 3px;font-family: 'festivo_lcbasic', Arial, Helvetica, sans-serif; text-transform:uppercase; font-size:26px; text-decoration:none; font-weight:normal; padding-top: 10px;"  onclick="check('myButton')"  onkeypress="check('myButton')"/>

<script type="text/javascript">

var password = ["#123, #345"];

function check(someID) {
    for(a = 0; a < password.length; a++) {
        if (document.getElementById(someID).value == password[a]) {
            top.location.href="enter.html";
        return;
        }
    } 
    location.href="incorrect.html";
}

</script>

Make sense?

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