简体   繁体   English

Javascript函数第二次尝试时不会转到下一个列表

[英]Javascript function not going to the next list on second try

I have problem with my javascript function. 我的javascript函数有问题。 It is not working as expected. 它没有按预期工作。 Look at this fiddle -> http://jsfiddle.net/NM3k6/1/ . 看这个小提琴-> http://jsfiddle.net/NM3k6/1/

Here I have questions with three selection each. 在这里,我有三个选择的问题。 If you select and answer through the normal flow. 如果您选择并通过正常流程进行回答。 It will give you both the 2 correct answer. 它会给您两个正确答案。 Now here is my problem, After you answer the first question, navigate back to first question again and select answer. 现在这是我的问题,在您回答第一个问题之后,再次导航回第一个问题并选择答案。 It will right away show result without going to the next question. 它会立即显示结果,而无需转到下一个问题。

Below is the code. 下面是代码。

<center>
<div id="your-curve-id">
  <fieldset class="questions" style="border: 1px solid #000; width:500px; margin: 20px">
    <h2 id="p1">1. Please select your Curve Id 1</h2>
    <ul id="ul1">
      <li class="option" name="Curve Id 1" value='Select 1A'>Answer 1</li>
      <li class="option" name="Curve Id 1" value='Select 1B'>Answer 2</li>
      <li class="option" name="Curve Id 1" value='Select 1C'>Answer 3</li>
    </ul>
  </fieldset>
  <fieldset class="questions" style="border: 1px solid #000; width:500px; margin: 20px">
    <h2 id="p2">2. Please select your Curve Id 2</h2>
    <ul id="ul2">
      <li class="option" name="Curve Id 2" value='Select 2A'>Answer 1</li>
      <li class="option" name="Curve Id 2" value='Select 2B'>Answer 2</li>
      <li class="option" name="Curve Id 2" value='Select 2C'>Answer 3</li>
    </ul>
  </fieldset>
</div>
</center>
<div id="results"></div>

here's the script.. 这是脚本

$(document).ready(function(){
    answers = new Object();
    var totalQuestions = $('.questions').size();

    $('.option').click(function(){
        console.log($(this).attr('value'));
        var answer = ($(this).attr('value'));
        var question = ($(this).attr('name'));
        answers[question] = answer;
        $('#count').html(Object.keys(answers).length + " / " + totalQuestions);
        next();
        back();
    });

    var currentQuestion = 0;
    $questions = $('.questions');
    $('ul#ul2').hide();

    function next(){
        $($questions.get(currentQuestion)).ready(function(){
            currentQuestion = currentQuestion + 1;
            if(currentQuestion == totalQuestions){
                //do stuff with the result
                var result = values();
                $('div#your-curve-id').hide();
                $('#results').html(result);

            }else{
                $($questions.get(currentQuestion)).show();
                $('ul#ul1').hide();
                $('ul#ul2').show('slow');
            }
        }); 
    }

    function values(){
        var res = "";
        var imahe = "";
        for (questions in answers)
        {
            if (answers.hasOwnProperty(questions)) {
                res += questions + '::' + answers[questions] + '<br />' + '<br />';
            }
        }
        return res;
    }

    function back(){
        $('h2#p1').click(function() {
            $('ul#ul1').show('slow');
            $('ul#ul2').hide();
        });
    }
});

Look at this fiddle -> http://jsfiddle.net/NM3k6/1/ . 看这个小提琴-> http://jsfiddle.net/NM3k6/1/

Short answer: you increment currentQuestion in next() , but you never decrement it in back() . 简短的答案:您currentQuestionnext()增加currentQuestion ,但决不能在back()中将其减一。

  function back() {
    $('h2#p1').click(function() {
      currentQuestion--;
      $('ul#ul1').show('slow');
      $('ul#ul2').hide();
    });
  }

For a much better answer, though, I suggest that you submit the same question, with that fix applied, to Code Review . 为了更好的答案,不过,我建议你提出同样的问题,与弄不好应用,以代码审查

You need to reduce currentQuestion when the user goes back: 您需要在用户返回时减少currentQuestion

function back(){
    $('h2#p1').click(function() {
        currentQuestion--;
        $('ul#ul1').show('slow');
        $('ul#ul2').hide();
    });
}

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM