簡體   English   中英

結合onclick和onkeypress

[英]Combine onclick and onkeypress

我有這個html表單,它實際上不會將數據發送到php文件,但是會將數據發送到javascipt函數。

如果我點擊按鈕工作正常,但是當我點擊輸入時沒有任何反應。

如果用鼠標點擊或者用戶點擊進入,我希望按鈕工作。

我該如何解決? 謝謝

       <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>

當您在text字段中鍵入用戶輸入enter-key時,我認為您正在嘗試檢查某些text 我假設你的input text是這樣的

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

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

並在script添加此功能

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

    }
}

完整的腳本:

<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>

要么:

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

腳本:

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

    }
}

你的代碼很糟糕,原因很多,我現在還沒有進入,但如果你查看你的javascript控制台(chrome / firefox),你會看到這個錯誤:

以下是我修復代碼的方法。 1.將ID“myButton”添加到2.將“myButton”傳遞給check()函數3.使用getElementById()從函數中獲取

<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>

說得通?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM