jQuery AJAX - 加载特定节点

时间:2017-08-17 15:38:57

标签: jquery ajax xml

我正在编写一个基于测验的AJAX。所以我想按顺序加载问题。但我只是不知道如何。

我的XML如下所示:

    <?xml version="1.0" encoding="UTF-8" ?>
    <multichoice>
        <question>
            <body>
                <question_title>If you have to stop in an emergency on a wet road you should</question_title>
                <question_correct_answers>a</question_correct_answers>
                <question_answer_a>Keep both of your hands on the steering wheel</question_answer_a>
                <question_answer_b>Brake very gently</question_answer_b>
                <question_answer_c>Use the foot brake and handbrake together</question_answer_c>
                <question_answer_d>Always change down through the gears</question_answer_d>
                <question_explination>The best advice about emergencies is to avoid them. You can minimise the chances of an emergency by watching the road well ahead and anticipating the actions of other road users. In an emergency, react quickly and brake firmly while keeping both hands on the wheel. You must make sure that you keep full control of your vehicle all the time, to help you do this, concentrate on clear spaces, not the things that you are trying to avoid.</question_explination>
            </body>
        </question>
        <question>
            <body>
                <question_title>Which one of the following things is most likely to distract a driver</question_title>
                <question_correct_answers>a</question_correct_answers>
                <question_answer_a>Using the demisters</question_answer_a>
                <question_answer_b>Stopping at red traffic lights</question_answer_b>
                <question_answer_c>Operating the indicators</question_answer_c>
                <question_answer_d>Dipping the headlights</question_answer_d>
                <question_explination>Driver distraction is one of the biggest causes of accidents. There are lots of things that can cause you to become distracted these range from getting lost, to eating snacks or smoking cigarettes. Something as simple as operating the radio or the car&#x27;s air conditioning can distract you from the road ahead.</question_explination>
            </body>
        </question>
</multichoice>

当你第一次点击页面时,我想加载第一个问题。用它的&#39; title,answer_a,answer_b等。

我确实需要知道问题的数量,所以我循环遍历问题节点。

但我想专门加载第一个问题节点,一旦下一个操作完成,请加载第二个节点,依此类推。

我目前的jQuery如下:

$.ajax(
        {
            type: "GET",
            url: question_path,
            cache: false,
            dataType: "xml",
            success: function(xml)
            {
                var current_question = 0;
                var num_questions = 0;

                // Get Specific Question...

                // Get Number Of Questions...
                $(xml).find('question').each(function()
                {
                    num_questions++;
                })

                // Set Page Title
                $('.page__title').html('Question '+ current_question +' of '+ num_questions);
            }
        });

由于

1 个答案:

答案 0 :(得分:0)

您可能希望将此数据存储在ajax之外并按相应的方式访问下一步,您会增加当前问题并使用类似$(question_xml).find('question').eq(current_question)

的内容

&#13;
&#13;
var current_question = 0;
var num_questions = 0;
var question_xml;
$.ajax(
        {
            type: "GET",
            url: question_path,
            cache: false,
            dataType: "xml",
            success: function(xml)
            {

                // add xml
                question_xml = xml;
          
                // Get Number Of Questions...
                $(xml).find('question').each(function()
                {
                    num_questions++;
                })

                // Set Page Title
                $('.page__title').html('Question '+ current_question +' of '+ num_questions);
            }
        });
&#13;
&#13;
&#13;