从2个或更多独立列表中随机选择元素

问题描述

我对所有这些东西都是陌生的。我找到了一个脚本here

我想做的是为我的博客创建多个小部件,人们可以在其中从已列出的元素中随机选择项目。

这里的问题是,尽管创建了4个不同的小部件,每个小部件具有不同的数组,但是当我单击“选择”时,结果总是来自这些小部件之一。我不知道如何重命名数组或小部件,以使它们看起来有所不同并可以单独工作。

这里有2个示例:

var up = document.getElementById('GFG_UP');
var down = document.getElementById('GFG_DOWN');

var arr = ["Alfa","Beta"];

$("#edit-field-service-sub-cat-value option[value=" + title + "]").hide();

up.innerHTML = "Click on the button to check " +
  "the type of element.<br><br>" + arr;

function GFG_Fun() {
  down.innerHTML =
    arr[Math.floor(Math.random() * arr.length)];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!doctype html>
<h1 style="color:green;">
  Choose
</h1>

<p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
</p>

<button id="button" onclick="GFG_Fun()">  
        click here  
    </button>

<p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;">
</p>

第二个就像

var up = document.getElementById('GFG_UP');
var down = document.getElementById('GFG_DOWN');

var arr = ["Gamma","Delta"];

$("#edit-field-service-sub-cat-value option[value=" + title + "]").hide();

up.innerHTML = "Click on the button to check " +
  "the type of element.<br><br>" + arr;

function GFG_Fun() {
  down.innerHTML =
    arr[Math.floor(Math.random() * arr.length)];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!doctype html>

<h1 style="color:green;">
  Choose
</h1>

<p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
</p>

<button id="button" onclick="GFG_Fun()">  
        click here  
    </button>

<p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;">
</p>

<script>
</script>

结果总是来自“ Alpha-Beta”列表,永远不会来自“ Gamma,Delta”。

我在做什么错?拜托...

当我仅使用一个小部件时,它工作正常,但我需要更多。如何在同一个小部件中添加2个数组,但只能从其中一个单独选择?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...