亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為輸入添加回車鍵?

如何為輸入添加回車鍵?

白板的微信 2022-12-09 19:11:07
我正在創建一個帶有一個輸入和一個按鈕的“算命先生”功能。用戶寫下一個問題,然后可以單擊該功能的按鈕來完成。然而,當用戶點擊回車時什么也沒有發生。我嘗試了在這里找到的一些解決方案,但沒有任何效果。我沒有做很多 JavaScript。謝謝!var outcomes = [  "It is certain",  "It is decidedly so",  "Without a doubt",  "Yes - definitely",  "You may rely on it",  "As I see it, yes",  "Most likely",  "Outlook good",  "Ask again later",  "Better not tell you now",  "Cannot predict now",  "Concentrate and ask again",  "Don’t count on it",  "No",  "My reply is no",  "My sources say yes",  "Outlook is not so good",  "Yes",  "Yes, it would benefit you",  "Reply hazy, try again",  "Very doubtful",  "Signs point to yes",  "No, it wouldn't benefit you"];function ask() {  var x = document.getElementById("input-box").value;  var sel = Math.floor(Math.random() * outcomes.length);  var b8 = outcomes[sel];  if (x.indexOf('?') + 1) {    document.getElementById("answer").innerHTML = b8;  } else {    document.getElementById("answer").innerHTML = "Try with a question mark (?)";  }}<div class="ask">  <input id="input-box">  <div class="button" onclick="ask()">    <img src="img/ask-1.png" onmouseover="this.src='img/ask-2.png';" onmouseout="this.src='img/ask-1.png';">  </div>  <p id="answer"></p></div>
查看完整描述

2 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

您可以將keyup事件偵聽器添加到您的輸入中。它應該是這樣的:


<!-- rest of your code -->


<input id="input-box" />


<script>

document.getElementById("input-box").addEventListener("keyup", function(event) {

   // 13 in the code of the enter key

   if(event.keyCode == 13){

      // your function

   }

})

</script>


<!-- rest of your code -->


查看完整回答
反對 回復 2022-12-09
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

如果你把它放在一個表單中并添加return false到onsubmit(這樣表單就不會重定向)。它應該工作。


代碼:


<div class="ask">

  <form onsubmit="ask(); return false;">

    <input id="input-box">

    <div class="button" onclick="ask()">

      <img src="img/ask-1.png" onmouseover="this.src='img/ask-2.png';" onmouseout="this.src='img/ask-1.png';">

    </div>

  </form>

  <p id="answer"></p>

</div>


查看完整回答
反對 回復 2022-12-09
  • 2 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號