当一个关键部分'form'标签不再存在时,如何测试提交的内容?

时间:2016-08-02 17:19:33

标签: javascript jquery

我最近更改了我的代码,使其更具响应性,因此搞砸了我的jQuery代码,以记录正确与错误答案的小测验。当问题位于HTML表单标记中时,我在下面发布的javascript用于工作正常。如果表单标签不再包围我的问题,我如何修改第二行(声明'answer1'变量)的jQuery代码才能正常工作?非常感谢。

HTML

<div class="intro-header2">
    <div class="container">
        <h1>The capital of Croatia is ...</h1>
        <p>&nbsp;</p>

        <div class="radio" style="margin-top: 0px;">
          <label><input type="radio" name="capital" value="zagreb" id="zagrebID">&nbsp; Zagreb</label>
        </div>
        <div class="radio" style="margin-top: 10px;">
          <label><input type="radio" name="capital" value="debrovnik">&nbsp; Debrovnik</label>
        </div>
        <div class="radio" style="margin-top: 10px;">
          <label><input type="radio" name="capital" value="makarska">&nbsp; Makarska</label>
        </div>
        <div class="radio" style="margin-top: 10px;">
          <label><input type="radio" name="capital" value="moscow">&nbsp; Moscow</label>
        </div>
        <div class="radio" style="margin-top: 20px;">
          <input type="button" class="btn btn-info" value="&nbsp; Next &nbsp;" id="NextID">
        </div>
    </div> <!--/.container-->
</div> <!--/.intro-header2-->

JS

$("#NextID").click(function(){
        var answer1 = ($('input[name=capital]:checked', '#myForm').val());
        if (answer1 == "zagreb") {
            var QuestionNumber = "Question 1, The capital of Croatia.";
            var QuizDesc = "Quiz questions on the country of Croatia.";
            var name = localStorage.getItem('name');
            var email = localStorage.getItem('email');

            //pulls the current counter from local storage
            var counter = localStorage.getItem('counter');
            //adds one to it
            var counter = parseInt(localStorage.getItem('counter')) + 1;
            //updates the global variable for counter
            setCounter(counter);

            passedQues(email, name, QuestionNumber, QuizDesc);
            document.location.replace("page3.html");
            }

        else if (!$("input[name='capital']:checked").val()) {
            alert('Nothing is checked!');
            return false;
    }

        else {
            var QuestionNumber = "Question 1, The capital of Croatia.";
            var QuizDesc = "Quiz questions on the country of Croatia.";
            var name = localStorage.getItem('name');
            var email = localStorage.getItem('email');

            //pulls the current counter from local storage
            var counter = localStorage.getItem('counter');
            //adds one to it
            var counter = parseInt(localStorage.getItem('counter')) + 0;
            //updates the global variable for counter
            setCounter(counter);

            failedQues(email, name, QuestionNumber, QuizDesc);
            document.location.replace("page3.html");
            }
    });

1 个答案:

答案 0 :(得分:1)

您不再拥有form元素,我认为它的id属性值为myForm

此行在ID为input

的元素中查找myForm的值
var answer1 = ($('input[name=capital]:checked', '#myForm').val());

此元素不再存在,因此找不到输入值。尝试将此行更改为:

var answer1 = ($('input[name=capital]:checked').val());