JavaScript简单提交无效

时间:2013-05-21 17:18:18

标签: javascript html

嘿伙计我在javascript遇到一些问题,我想知道你是否可以帮助我?

这是我的 HTML 代码

<div class="Answer1"> 
    <form name="form">
        Enter your answer here :
        <input type="text" size="10" name="answer" value="">
        <input type="button" value="Check"  onclick="result();">
</form>
</div>

<!--2st sum -->
<div class="Answer2">
    <form name="form">
        Enter your answer here : 
        <input type="text" size="10" name="answer" value="">
        <input type="button" value="Check"  onclick="result2();">
    </form>
</div>

这是我的 javascript

function result() {
    var  score = (document.form.answer.value);
    if(score == 8) {
        document.location.href="CorrectAdditionAnswer.html"
} else {
        document.location.href="IncorrectAddition.html"
    }
}

function result2() {
    var  score = (document.form.answer.value);
    if(score == 8) {
        document.location.href="CorrectAdditionAnswer2.html"
    } else {
        document.location.href="IncorrectAddition.html"
    }
}

如果我评论出其中一个它完全没问题,但如果它没有评论它不起作用:(我不明白我做错了什么:(

3 个答案:

答案 0 :(得分:1)

form属性有两个nameinput属性值相同,而name有两个{{1}} {。}}。

答案 1 :(得分:0)

要将几个元素的相同ID设置为'gridlock'中的javascript。使用唯一的id名称标识您的元素通常可以更快,更轻松地使DOM / Javascript / jQuery更快地找到节点/元素。

如果所有元素都在同一页面中,则此格式是适当的方法。

 <form name="form1">
    Enter your answer here :
    <input type="text" size="10" id="answer1" name="answer1" value="">

    <input type="button" value="Check" onclick="result1();">
    </form>

</div>

<!--2st sum -->
<div class="Answer2">

    <form name="form2">
    Enter your answer here : 
    <input type="text" size="10" id="answer2" name="answer2" value="">

    <input type="button" value="Check"  onclick="result2();">
    </form>

答案 2 :(得分:0)

js

     function result()
{
    var  score =document.getElementsByName('answer1')[0].value;

    if(score == 8)
    {
      document.getElementsByName('form1')[0].action = "http://www.wordpress.com";
      document.getElementsByName('form1')[0].submit();
    }
    else
    {   
      document.getElementsByName('form1')[0].action = "http://www.bing.com";
      document.getElementsByName('form2')[0].submit();
    }
}

function result2()
{
    var  score = document.getElementsByName('answer2')[0].value;
    if(score == 8)
    {
        document.getElementsByName('form2')[0].action = "http://www.wordpress.com";
      document.getElementsByName('form2')[0].submit();
    }
    else
    {
        document.getElementsByName('form2')[0].action = "http://www.bing.com";
      document.getElementsByName('form2')[0].submit();
    }
}

HTML

<div>  
<form name="form1">

        Enter your answer here :
        <input type="text" size="10" name="answer1" value="">

        <input type="button" value="Check"  onclick=" result();">
        </form>

</div>

<!--2st sum -->
<div class="Answer2">

        <form name="form2">
        Enter your answer here : 
        <input type="text" size="10" name="answer2" value="">

        <input type="button" value="Check"  onclick="  result2();">
        </form>

</div>