我正在做一个随机生成西班牙语单词的小项目,用户输入英文翻译,然后点击查看它是对还是错。目前它处于早期阶段,但我无法正确匹配逻辑。到目前为止,我有: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 知识非常生疏。有人可以帮忙吗?
1 回答

守着一只汪
TA贡献1872条经验 获得超4个赞
你不应该知道你问的是哪个问题吗?只需检查它是否匹配。
if ( phrases[ spanish.question ] == answer ) { ... }
添加回答
举报
0/150
提交
取消