尝试使用html,js和表单创建一个非常简单的测验

时间:2018-04-13 03:06:54

标签: javascript html forms

如果问题1是" 1"或问题2是" 4"我需要它显示绿色。当调用标记测验时,否则为红色。这个代码我哪里错了? js位于外部文件中。

Html:

<body>
<table>
    <tr>
        <th>Questions</th>
        <th>Answer</th>
    </tr>
    <tr>
        <td id="1">
            Water is
            <ol>
                <li> Wet</li>
                <li> Ordinary</li>
                <li>Purify </li>
                <li> Water</li>
            </ol>
        </td>
        <td>
            <form name="question1" action="www.quizMarker.ca">
                <input type="text" name="quest1" placeholder="Enter Answer"> 
            </form>

        </td>
    </tr>
    <tr>
        <td id="2">
            Fire is
            <ol>
                <li> Wet</li>
                <li> Ordinary</li>
                <li>Purify </li>
                <li> Hot</li>
            </ol>
        </td>
        <td>
            <form name="question2" ction="www.quizMarker.ca">
                <input type="text" name="quest1" placeholder="Enter Answer"> 
            </form>

        </td>
    </tr>
</table>
<button type="button" onclick="markQuiz()">Mark Quiz</button>

js:

function markQuiz()
{
var q1 = document.forms['question1'];
var q1Ans = q1.elements['quest1'].value;

var q2 = document.forms['question2'];
var q2Ans = q2.elements['quest2'].value;

var row1 = document.getElementById("1");
var row2 = document.getElementById("2");

if(q1Ans = "1")
    {
        row1.style.backgroundColor = "green";
    }
else()
    {
        row1.style.backgroundColor = "red";
    }

if(q2Ans = "4")
    {
        row2.style.backgroundColor = "green";
    }
else()
    {
        row2.style.backgroundColor = "red";
    }
}  

现在,当调用时,markQuiz没有做任何事情。此外,如果有更简单的方法来完成此任务,建议将有所帮助。

3 个答案:

答案 0 :(得分:0)

你可能需要&#34; ==&#34;或&#34; ===&#34;而不是&#34; =&#34;,这意味着分配。

 public ActionResult Index() { var x = objrepo.GetCountry();return View(x); }

答案 1 :(得分:0)

代码中有一个额外的=符号

if(q1Ans = "1")
    {
        row1.style.backgroundColor = "green";
    }

正确的语法是:

if(q1Ans == "1")
        {
            row1.style.backgroundColor = "green";
        }

答案 2 :(得分:0)

请检查我所做的更改

function markQuiz()
{
var q1Ans = document.forms['question1'].quest1.value;
var q1Ans = document.forms['question2'].quest2.value;

// PLease change the ids of the td ttag
var row1 = document.getElementById("table1");
var row2 = document.getElementById("table2");

if(q1Ans === "1")
    {
        row1.style.backgroundColor = "green";
    }
else
    {
        row1.style.backgroundColor = "red";
    }

if(q2Ans === "4")
    {
        row2.style.backgroundColor = "green";
    }
else
    {
        row2.style.backgroundColor = "red";
    }
}