【问题标题】:I have a jQuery quiz that works but it hides the wrong answers to the previous questions once an a correct answer is made我有一个有效的 jQuery 测验,但是一旦做出正确答案,它就会隐藏对先前问题的错误答案
【发布时间】:2020-02-18 23:00:17
【问题描述】:

我有一个有效的测验,但一旦做出正确答案,它就会隐藏对先前问题的错误答案。我希望出现 6 个问题的结果,即使其中一些不正确。我讨厌重复相同的代码 6 次。

https://designaire.com/test1/health.html

<script>
    $(function() {
          $('.exp').hide();
          $('.wrong').hide();
          $('input[name="test"]').on('click', function() {
            var el = $(this);
            if (el.val() == 'ans') {
              el.parents('.q').nextAll('.exp').first().show();
              el.parents('.q').nextAll('.wrong').first().hide();
              $('.wrong').hide();
            } else {
              el.parents('.q').nextAll('.wrong').first().show();
              el.parents('.q').nextAll('.exp').first().hide();
            }
          })
        });
    </script>
    <form class="q">
    1. Janelle is excitedly telling her friend Reina how her job interview went. Reina keeps texting her boyfriend.
      <div class="answer">
    <input name="test" type="radio" value="inc" />
    Respect
    <input name="test" type="radio" value="ans" />
    Disrespect
    </div>
    </form>
    <div class="exp">&#9733; Good Job!</div>
    <div class="wrong">Take another look at the examples in this lesson.</div></div>

【问题讨论】:

    标签: javascript jquery arrays google-form-quiz


    【解决方案1】:

    你有一个条件,它隐藏了所有错误答案的元素,把它去掉。

    <script>
        $(function() {
              $('.exp').hide();
              $('.wrong').hide();
              $('input[name="test"]').on('click', function() {
                var el = $(this);
                if (el.val() == 'ans') {
                  el.parents('.q').nextAll('.exp').first().show();
                  el.parents('.q').nextAll('.wrong').first().hide();
                  $('.wrong').hide(); // => remove this line
                } else {
                  el.parents('.q').nextAll('.wrong').first().show();
                  el.parents('.q').nextAll('.exp').first().hide();
                }
              })
            });
        </script>
    

    例如:https://jsfiddle.net/L0u798by/

    【讨论】:

    • 然后你可以点击一个正确和错误的答案,反馈都会出现。
    • 不确定我是否理解。您提到“我希望出现 6 个问题的结果,即使其中一些不正确”。你能详细说明一下吗?
    • 这就是它如何处理 6 个问题。 designaire.com/test1/health.html 如果你点击错误的答案,你会得到反馈,然后你点击下一个问题并得到正确的答案,之前的反馈会被隐藏。我希望旧的反馈保留在那里。
    • @designaire 你看过小提琴吗?它正在做你提到的事情。
    • 它似乎确实适用于小提琴。当我尝试时,两个文件同时出现。谢谢,我再看看。谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    相关资源
    最近更新 更多