【问题标题】:Shuffling Column of table using JQuery使用 JQuery 洗牌表的列
【发布时间】:2020-05-03 12:47:56
【问题描述】:

尝试打乱表格的列。

尝试了下面的代码,但它根本没有洗牌。

我需要洗牌前四个选项。

window.onload = function() {
  $("div.question_div").randomize("span.tdran");
};


(function($) {

  $.fn.randomize = function(tree, childElem) {
    return this.each(function() {
      var $this = $(this);
      if (tree) $this = $(this).find(tree);
      var unsortedElems = $this.children(childElem);
      var elems = unsortedElems.clone();

      elems.sort(function() {
        return (Math.round(Math.random()) - 0.5);
      });

      for (var i = 0; i < elems.length; i++)
        unsortedElems.eq(i).replaceWith(elems[i]);
    });
  };

})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Question 3
<div class="question_div">
  <table>
    <tr>
      <td colspan="4">
        <p>In a camp, there is a meal for 200 children or 120 men. If 150 children have taken the meal, how many men will be served with the remaining meal?</p>
      </td>
    </tr>
    <tr>
      <span class="tdran">
        <td><input type="radio" name="radio3" id="checka3" value="A"></td>
        <td><label for="checka3">
            <p>31</p>
          </label></td>
      </span>
      <span class="tdran">
        <td><input type="radio" name="radio3" id="checkb3" value="B"></td>
        <td><label for="checkb3">
            <p>30</p>
          </label></td>
      </span>
    </tr>
    <tr>
      <span class="tdran">
        <td><input type="radio" name="radio3" id="checkc3" value="C"></td>
        <td><label for="checkc3">
            <p>29</p>
          </label></td>
      </span>
      <span class="tdran">
        <td><input type="radio" name="radio3" id="checkd3" value="D"></td>
        <td><label for="checkd3">
            <p>35</p>
          </label></td>
      </span>
    </tr>
    <tr>
      <td><input type="radio" name="radio3" id="checke3" value="E"></td>
      <td><label for="checke3">
          <p>None of these</p>
        </label></td>
    </tr>
  </table>
</div>
<hr>
</br>

尝试打乱表格的列。

尝试了下面的代码,但它根本没有洗牌。

我需要洗牌前四个选项。


我能否获得额外的附注,即如何删除两个选项之间以及单选按钮和值之间的空格?

【问题讨论】:

    标签: javascript jquery html-table shuffle


    【解决方案1】:

    您的 HTML 标记无效 - 不能用 &lt;span&gt; 包装 &lt;td&gt;&lt;tr&gt; 的唯一允许子级是 &lt;td&gt;。虽然可以随机更改表格单元格的顺序,但我建议改用&lt;div&gt;s,因为它更容易。

    window.onload = function() {
      randomize();
    };
    
    function randomize() {
      let questions = $(".question_div");
      let divCollection = questions.find(".ran");
      let divs  = Array.from(divCollection);
      shuffleArray(divs);
      for (const div of divs) {
        questions.append(div);
      }
    }
    
    function shuffleArray(array) {
      for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
    }
    label {
      display: inline-block;
      margin-left: 5px;
    }
    
    .ran {
      float: left;
      width: 200px;
    }
    
    div:nth-child(even) {
      clear: left;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Question 3
    <div class="question_div">
      <p>In a camp, there is a meal for 200 children or 120 men. If 150 children have taken the meal, how many men will be served with the remaining meal?</p>
      <div class="ran">
        <input type="radio" name="radio3" id="checka3" value="A">
        <label for="checka3">
          <p>31</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checkb3" value="B"><label for="checkb3">
          <p>30</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checkc3" value="C"><label for="checkc3">
          <p>29</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checkd3" value="D"><label for="checkd3">
          <p>35</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checke3" value="E"><label for="checke3">
          <p>None of these</p>
        </label>
      </div>
    </div>

    要获得更多列之间的距离,只需增加类.ran 的宽度即可。工作Fiddle。我用这个答案作为我的答案:Javascript: Shuffle table rows

    更新:建议的解决方案仅适用于一个问题。为了处理更多问题,我使用each()进行了以下调整:

    window.onload = function() {
      randomize();
    };
    
    function randomize() {
      let questions = $(".question_div");
    
      questions.each(function() {
        let divCollection = $(this).find(".ran");
        let divs = Array.from(divCollection);
        shuffleArray(divs);
        for (const div of divs) {
          $(this).append(div);
        }
      });
    
    
    }
    
    function shuffleArray(array) {
      for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
    }
    label {
      display: inline-block;
      margin-left: 5px;
    }
    
    .ran {
      float: left;
      width: 200px;
    }
    
    div.ran:nth-child(odd) {
      clear: left;
    }
    .question_div {
      display:inline-block;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div class="question_div">
    <p>
    Question 4
    </p>
      <p>In a camp, there is a meal for 200 children or 120 men. If 150 children have taken the meal, how many men will be served with the remaining meal?</p>
      <div class="ran">
        <input type="radio" name="radio3" id="checka3" value="A">
        <label for="checka3">
          <p>31</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checkb3" value="B"><label for="checkb3">
          <p>30</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checkc3" value="C"><label for="checkc3">
          <p>29</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checkd3" value="D"><label for="checkd3">
          <p>35</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio3" id="checke3" value="E"><label for="checke3">
          <p>None of these</p>
        </label>
      </div>
    
    </div>
    
    <div class="question_div">
    <p>
    Question 4
    </p>
    <p>In a camp, there is a meal for 200 children or 120 men. If 150 children have taken the meal, how many men will be served with the remaining meal?</p>
      <div class="ran">
        <input type="radio" name="radio4" id="checka4" value="A">
        <label for="checka4">
          <p>31</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio4" id="checkb4" value="B"><label for="checkb4">
          <p>30</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio4" id="checkc4" value="C"><label for="checkc4">
          <p>29</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio4" id="checkd4" value="D"><label for="checkd4">
          <p>35</p>
        </label>
      </div>
      <div class="ran"><input type="radio" name="radio4" id="checke4" value="E"><label for="checke4">
          <p>None of these</p>
        </label>
      </div>
    
    </div>

    【讨论】:

    • 这在只有一个问题时有效。但当有超过 1 个问题时,无法准确工作。我试过了:jsfiddle.net/640rp3nL
    • 另外,div:nth-child(even) { clear: left; } 会更改文件中存在的所有其他 div。
    • @vipejo 我已经调整了您的 Fiddle 以处理更多问题。我还调整了 div:nth-child() 选择器以仅影响具有类 .ran 的 div,因此它不应干扰您页面上的其他 div。 jsfiddle.net/uo2p4emn
    • @vipejo 我已经调整了你的小提琴,所以问题不再重复了。 jsfiddle.net/71db50rj
    • @vipejo 我不确定是什么导致了这个错误,但我刚刚重写了 Fiddle,现在它工作正常:jsfiddle.net/roj83eg6
    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 2020-03-02
    • 1970-01-01
    • 2012-02-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多