如果,那么对于琐事的陈述

If then statement for trivia

本文关键字:于琐事 陈述 如果      更新时间:2023-09-26

我刚刚开始,被困在一个看似基本的If then语句上。希望有人能告诉我他们的想法。

我想设计一个小问题,让用户猜测选举年。如果他们答对了,就会收到"你答对了!"的提示;出错时发出"你错了"的警报。然而,似乎无论提交的答案是什么,它总是返回一个"你是对的"警报。

在过去的三个小时里我一直在绞尽脑汁,所以任何反馈都是非常感谢的。谢谢你! !

<!doctype html>
<html>  <title>CodePlayer</title>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jqueryui.min.js"></script>

    <style>
    .container {
        margin: 50px 0 0 50px;
    }

    </style>

</head>

<body>
    <div class="container">
        When was Obama elected President?
        <input id="answerswer" />
        <button id="submit">Submit Your Answer</button>

    </div>

    <script>

        var electionGuess = document.getElementById("answerswer").value;
        var electionYear = 2008;

        document.getElementById("submit").onclick=function() {
            if (electionGuess = electionYear) {
                alert("You're right!");
            } else if (electionGuess != electionYear) {
                alert("No you're wrong")
            }
        }
    </script>


</body>
</html>

此操作符

if (electionGuess = electionYear) {
应该

if (electionGuess == electionYear) {

因为=本身就是一个赋值操作符