Javascript-从5个列表中随机选择3个不重复的div,并将它们显示在页面上

Javascript-从5个列表中随机选择3个不重复的div,并将它们显示在页面上,javascript,jquery,html,random,Javascript,Jquery,Html,Random,目前,我正在将div加载到.test1、2和3中,如下所示: jQuery(function($){ $(".test1").load("/?page=owner_testimonials&gridID=1 #showGrid1Content"); $(".test2").load("/?page=owner_testimonials&gridID=2 #showGrid2Content"); $(".test3").load("/?page=owner_

目前,我正在将div加载到.test1、2和3中,如下所示:

jQuery(function($){
    $(".test1").load("/?page=owner_testimonials&gridID=1 #showGrid1Content");
    $(".test2").load("/?page=owner_testimonials&gridID=2 #showGrid2Content");
    $(".test3").load("/?page=owner_testimonials&gridID=3 #showGrid3Content");});
现在,我正在将3份推荐信加载到“.test1”、“.test2”、“.test3”三个插槽中,但您可以看到推荐信不是随机选择的,它们只是按顺序拉取:推荐信1(showGrid1Content)中的内容是从推荐信的来源页面中加载的(/?page=owner_-Testionals&gridID=1)

相反,我想加载以下内容之一:

“/?page=owner_Certifications&gridID=1#ShowGrid1内容”

“/?page=owner_Certifications&gridID=2#showGrid2Content”

“/?page=owner_Certifications&gridID=3#showGrid3Content”

“/?page=owner_Certifications&gridID=4#showGrid4Content”

“/?page=owner_Certifications&gridID=5#ShowGrid5内容”

随机分为.test1、.test2和.test3,但我不希望它们中的任何两个是相同的(例如,我不希望.test1有gridID4,而.test3也有gridID4)


如何使用jQuery或Javascript实现这一点?

创建一个包含所有所需睾丸的数组,然后使用Math.floor(Math.random()*testimonalArray.length)

这是你睾丸的指数

用testimonalArray.拼接(索引1)将其移除

样本:

var testimonalArray = [
   '/?page=owner_testimonials&gridID=1 #showGrid1Content',
   '/?page=owner_testimonials&gridID=2 #showGrid2Content',
   '/?page=owner_testimonials&gridID=3 #showGrid3Content',
   '/?page=owner_testimonials&gridID=4 #showGrid4Content',
   '/?page=owner_testimonials&gridID=5 #showGrid5Content'
];

function randomize() {
    var idx = Math.floor(Math.random()*testimonalArray.length);
    var testimonal = testimonalArray[idx];
    testimonalArray.splice(idx, 1);
    return testimonal;
}

var elements = ['.test1', '.test2', '.test3'];
for(var i = 0; i < 3; i++) {
    $(elements[i]).load(randomize());
}
var testimonalArray=[
“/?page=owner_Certifications&gridID=1#ShowGrid1内容”,
“/?page=owner_Certifications&gridID=2#showGrid2Content”,
“/?page=owner_Certifications&gridID=3#showGrid3Content”,
“/?page=owner_Certifications&gridID=4#showGrid4Content”,
“/?page=owner_Certifications&gridID=5#ShowGrid5内容”
];
函数随机化(){
var idx=Math.floor(Math.random()*testimonalArray.length);
var testimonal=testimonalArray[idx];
睾丸染色体剪接(idx,1);
返回睾丸;
}
var元素=['.test1'、'.test2'、'.test3'];
对于(变量i=0;i<3;i++){
$(元素[i])。加载(随机化());
}

我不能投票支持夏威夷的答案,因为我还没有15个名声,但我可以链接到一个做同样事情的JSFIDLE

var选项=[1,2,3,4,5];
var NumberOfEstimations=3;

对于(var i=1;i将它们放入数组中,选择一个随机索引,删除该索引,然后重复。将ID值放入数组中,并使用大量随机数选择器之一。测试并发性将有点困难。首先生成数字列表,然后加载页面
var options = [1, 2, 3, 4, 5];
var numberOfTestimonials = 3;

for (var i=1; i<=numberOfTestimonials; i++) {
    var randomIndex = getRandomIndex(options);

    $('#result').append(options[randomIndex]);
    options.splice(randomIndex, 1);
}

function getRandomIndex(options) {
    return Math.floor(Math.random() * options.length);    
}