我正在做一個隨機生成西班牙語單詞的小項目,用戶輸入英文翻譯,然后點擊查看它是對還是錯。目前它處于早期階段,但我無法正確匹配邏輯。到目前為止,我有:Spanish_Phrases.prototype.calculate = function(){ const phrases = { hola: "hello", adios: "bye", bonita: "bonita", }; this.answer = (document.getElementById("answer").value); if(this.answer == Object.values(phrases)) { alert("Correct") } else { alert("Try again"); }}HTML在這里:<!DOCTYPE html><html><head> <title>Spanish</title></head><body> <center> <script src="./src/spanish.js"> </script> <link href="skeleton.css" rel="stylesheet"> <section> <h2>Spanish</h2> <h3 id="question"></h3> <button type="button" id="questionbutton">Get question</button> <form action="#" id="form" name="form" onsubmit="return false;"> Enter answer<br> <input id="answer" name="answer" type="text"><br> <br> <input id="submit" type="submit" value="Submit"> </form> </section> </center> <script src="https://code.jquery.com/jquery-3.4.1.min.js"> </script> <script> $(function () { var spanish = new Spanish_Phrases(); $('#questionbutton').on('click', function(){ spanish.randomize(); $('#question').text(spanish.question); }) $('#submit').on('click', function() { spanish.calculate(); });});</script>答案是用戶在表單中提交的內容。如果該答案值正確匹配其相應的鍵,我希望它顯示正確,但無法弄清楚如何。抱歉,我很欣賞這可能是非常基礎的,但我的 javascript 知識非常生疏。有人可以幫忙嗎?
如何檢查一個值是否與它在 Javascript 中的哈希鍵匹配
狐的傳說
2022-06-16 10:14:13