【问题标题】:Missing elements when randomizing order of list items in page随机化页面中列表项的顺序时缺少元素
【发布时间】:2016-10-29 08:40:14
【问题描述】:

我正在尝试为一个教育网站开发一个小测试,我想随机化问题和练习的顺序,然后用一组单选按钮在每个问题下的答案顺序下线。我已经尝试了我在两个单独的问题(即this onethis one)上找到的代码,尽管两个答案实际上是相同的。我正在使用的代码在某种程度上可以满足我的要求,但有时我的部分测试会丢失(大多数时候我得到 5 个问题和 2-3 个练习,而不是 6 个和 4 个)。我该如何纠正? JSFiddle 中的完整代码示例(来自后端的一些不相关的东西,忽略它们 - 内容是占位符,使调试更容易),以及下面的 javascript/jQuery 代码:

$(document).ready(function () {
        var questions = $('.question');
        for (var i = 0; i < questions.length; i++) {
            var target = Math.floor(Math.random() * questions.length - 1) + 1;
            var target2 = Math.floor(Math.random() * questions.length - 1) + 1;
            questions.eq(target).before(questions.eq(target2));
        }

        var exercises = $(".exercise");
        for (var j = 0; j < exercises.length; j++) {
            var target = Math.floor(Math.random() * exercises.length - 1) + 1;
            var target2 = Math.floor(Math.random() * exercises.length - 1) + 1;
            exercises.eq(target).before(exercises.eq(target2));
        }
    });

附注#1:网站的后端是用 Asp.Net 和 C# 构建的,如果这与问题有关的话。

附注#2: 拉小提琴四五次,计算问题和练习的数量以重现问题。

【问题讨论】:

    标签: javascript jquery html random


    【解决方案1】:

    我更改了您的代码,使targettarget2 不同:

    JavaScript

    var questions = $('.question');
    for (var i = 0; i < questions.length; i++) {
      var target = Math.floor(Math.random() * questions.length - 1) + 1;
      var target2 = target;
      while(target2 === target) {
        target2 = Math.floor(Math.random() * questions.length - 1) + 1;
      }
      questions.eq(target).before(questions.eq(target2));
    }
    
    var exercises = $(".exercise");
    for (var j = 0; j < exercises.length; j++) {
      var target = Math.floor(Math.random() * exercises.length - 1) + 1;
      var target2 = target;
      while(target2 === target) {
        target2 = Math.floor(Math.random() * exercises.length - 1) + 1;
      }
      exercises.eq(target).before(exercises.eq(target2));
    }
    

    fiddle

    【讨论】:

    • 是的,这似乎解决了这个问题。所以问题是有时两个随机目标是相同的,完全忘记检查了。谢谢!
    • 是的,很难在它自己之前添加一个项目:)
    【解决方案2】:

    我将假设所有 .question / .exercise 元素都在同一个父元素中。

    我会采用的方法是:

    1. 以数组形式获取问题/练习并将它们分离

    2. 随机化数组,在any of serveral ways addressed in this question's answers

    3. 附加它们

    例如:

    function randomize(selector) {
      // Get the elements and their parent
      var elements = $(selector);
      var parent = elements.eq(1).parent();
      
      // Detach, get array
      elements = elements.detach().get();
      
      // Shuffle it
      shuffle(elements);
      
      // Attach
      parent.append(elements);
    }
    
    // From https://stackoverflow.com/a/2450976/157247
    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex;
    
      // While there remain elements to shuffle...
      while (0 !== currentIndex) {
    
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
    
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }
    
      return array;
    }
    
    randomize(".question");
    randomize(".exercise");
    <ul>
      <li class="question">Question 1</li>
      <li class="question">Question 2</li>
      <li class="question">Question 3</li>
      <li class="question">Question 4</li>
      <li class="question">Question 5</li>
    </ul>
    Exercises:
    <ul>
      <li class="exercise">Exercise 1</li>
      <li class="exercise">Exercise 2</li>
      <li class="exercise">Exercise 3</li>
      <li class="exercise">Exercise 4</li>
      <li class="exercise">Exercise 5</li>
      <li class="exercise">Exercise 6</li>
    </ul>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    【讨论】:

    • 非常感谢!这就像一个魅力,我想我可以使用基于 name 属性的选择器在不同问题的单选按钮集上再次运行它。
    • 为什么这不是公认的答案:我相信这种方法优于我的原始代码,因为它更通用,并且可以提供更通用的解决方案,无需重写任何代码,但手头的问题是我的代码有一些我无法弄清楚的问题,我需要知道那是什么以及如何解决它。因此,我接受@Arg0n 的回答,因为它对我来说更有启发性,并教会了我一些更有价值的东西。
    • @AngelosChalaris:很公平! :-)
    猜你喜欢
    • 2020-06-28
    • 2011-10-27
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2016-05-01
    • 2011-01-23
    • 2023-03-29
    相关资源
    最近更新 更多