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

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

有沒有辦法檢查一個變量的值是否與另一個變量的值相同?

有沒有辦法檢查一個變量的值是否與另一個變量的值相同?

30秒到達戰場 2022-07-15 09:47:23
我正在嘗試制作密碼系統,但我不知道密碼。無論密碼是什么,它都包含在一個變量中。我在嘗試:<input type="password" id="pass"> <button onclick="pass()">Go</button><script>var password = document.getElementById("pass").value;var answer = "the password"function pass() {if (password == answer) {window.location.replace("http://example.com");}else {document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";}}</script>它沒有用。有沒有辦法檢查一個變量的值是否與另一個變量的值相同?謝謝!
查看完整描述

3 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

你只是有一個“時間”問題:你應該在點擊按鈕的那一刻讀取用戶輸入的值,你需要將你的var password = document.getElementById("pass").value行移到函數調用中。


const answer = "the password";

function pass() {

  const password = document.getElementById("pass").value;

  if (password == answer) {

    document.getElementById("note").innerHTML = "<p>Good password!</p>";

  }

  else {

    document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";

  }

}

<input type="password" id="pass"> 

<button onclick="pass()">Go</button>

<div id="note"></div>


查看完整回答
反對 回復 2022-07-15
?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

您應該在函數內獲取輸入的值,如下所示:


var answer = "the password"

function pass() {

     var password = document.getElementById("pass").value;

     if (password == answer) 

          window.location.replace("http://example.com");

     else 

          document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";

}

<input type="password" id="pass" /> 

<button onclick="pass()">Go</button>

<div id="note"></div>


查看完整回答
反對 回復 2022-07-15
?
米脂

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

你好,有同樣的錯誤,試試這個


var answer = "the password";

function pass() {

    var password = document.getElementById("pass").value;

    if (password === answer) {

        window.location.replace("http://example.com");

    }

    else {

        document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";

    }

}


查看完整回答
反對 回復 2022-07-15
  • 3 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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