JQuery Mobile:随机排序复选框,除了一个

时间:2013-05-28 18:59:36

标签: sorting jquery-mobile random checkbox

在一项调查中,我需要显示几个以随机方式显示的复选框,但其中一个必须保持不变,因为我需要捕获一个文本,例如

What is your favorite color?

( ) Red
( ) Yellow
( ) Blue
Other, please specify:____________________

--------------

What is your favorite color?

( ) Yellow
( ) Red
( ) Blue
( ) Other, please specify:____________________


--------------

What is your favorite color?

( ) Blue
( ) Yellow
( ) Red
( ) Other, please specify:____________________

我有一个有效的代码,但我无法保留最后一个选项......

<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<meta charset=utf-8 />
<title>Semi-Random Checkboxes</title>
</head>
<body>
  <fieldset id="checkboxes" data-role="controlgroup">
  <label><input type="checkbox" value="red">Red</label>
  <label><input type="checkbox" value="blue">Blue</label>
  <label><input type="checkbox" value="yellow">Yellow</label>
  <label><input type="checkbox" value="Other">Other, specify</label><input type="text" value="text">
  </fieldset>

  <script>
    (function($) {
      var container = $("#checkboxes");
      var cbs = container.children("label");
      var index;
      for (index = 0; index < cbs.length; ++index) {
          $(cbs[Math.floor(Math.random() * cbs.length)]).appendTo(container);
      }
    })(jQuery);
  </script>

</body>
</html>

0 个答案:

没有答案