选择题选项随机排序的jquery实现

要求答题的A,B,C,D答案能够随机变化位置,也就是A,B,C,D答案内容每次刷新都应该是不同的。

实现前的html源代码:

1
2
3
4
5
6
< div  class = "col-md-12"  id = "datiOption" >
                     < p  class = "bigFont"  data-bind = "visible:Type()!='判断'" >< input  type = "checkbox"  name = "chk"  data-bind = "visible:Type()=='多选',value:Option1"  id = "sela"  />< span  class = "seloption" ></ span >< span  data-bind = "text:Option1" ></ span ></ p >
                     < p  class = "bigFont"  data-bind = "visible:Type()!='判断'" >< input  type = "checkbox"  name = "chk"  data-bind = "visible:Type()=='多选',value:Option2"  id = "selb"  />< span  class = "seloption" ></ span >< span  data-bind = "text:Option2" ></ span ></ p >
                     < p  class = "bigFont"  data-bind = "visible:Type()!='判断'" >< input  type = "checkbox"  name = "chk"  data-bind = "visible:Type()=='多选',value:Option3"  id = "selc"  />< span  class = "seloption" ></ span >< span  data-bind = "text:Option3" ></ span ></ p >
                     < p  class = "bigFont"  data-bind = "visible:Type()!='判断'" >< input  type = "checkbox"  name = "chk"  data-bind = "visible:Type()=='多选',value:Option4"  id = "seld"  />< span  class = "seloption" ></ span >< span  data-bind = "text:Option4" ></ span ></ p >
                 </ div >


实现效果所需的jquery代码:

在javascript的$(function(){

})中增加如下代码:

//选项随机排序
           $("#datiOption p").each(function(index) {
               if (parseInt(Math.random() * 2) == 0) {
                   $(this).prependTo($(this).parent());
               }	
           });
           //选项在随机排序后再根据新排序重新赋值A,B,C,D 
           $("#datiOption p").each(function (index) {
               if(index==0) {
                   $(".seloption").eq(0).text("A、");
               }
               else if (index == 1) {
                   $(".seloption").eq(1).text("B、");
               }
               else if (index == 2) {
                   $(".seloption").eq(2).text("C、");
               }
               else if (index == 3) {
                   $(".seloption").eq(3).text("D、");
               }
           });

其中第一部分代码实现了随机排序。

第二部分代码根据随机排序后的代码重新更新A,B,C,D的显示。因为A,B,C,D你是不能写死的,否则随机排序后,看到的就不是按照A,B,C,D顺序排列了



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值