javascript javascript按钮从数组中选择随机项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16753876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:51:49  来源:igfitidea点击:

javascript button to pick random item from array

javascriptbutton

提问by user2419017

Please can someone help. I am trying to create a basic button in javascript that when clicked picks a value at random from my array and display it on the screen, each time the button is clicked it should pick a new item from the array. I know how to write the array

请有人帮忙。我正在尝试在 javascript 中创建一个基本按钮,当单击它时,它会从我的数组中随机选取一个值并将其显示在屏幕上,每次单击该按钮时,它都应该从数组中选取一个新项目。我知道如何写数组

var myarray = new Array("item1", "item2", "item3");

I just dont know how to do the button part. Any help would be great. I know it may be easier to do this in jQuery, but I really want to get my head round javascript before I tackle jQuery (please be gentle I am new to this lol)

我只是不知道如何做按钮部分。任何帮助都会很棒。我知道在 jQuery 中执行此操作可能更容易,但我真的很想在处理 jQuery 之前先了解一下 javascript(请保持温和,我是这个哈哈的新手)

回答by Sachin

You can call a function on button click to print the value like this

您可以在按钮单击时调用函数以打印这样的值

<input type="button" id="btnSearch" value="Search" onclick="GetValue();" />
<p id="message" ></p>

JS

JS

function GetValue()
{
    var myarray= new Array("item1","item2","item3");
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    //alert(random);
    document.getElementById("message").innerHTML=random;
}

JS Fiddle Demo

JS小提琴演示

回答by Guanxi

Use Random function of javascript to generate a random number between upper and lower limit of your array. Then use

使用 javascript 的 Random 函数在数组的上限和下限之间生成一个随机数。然后使用

myarray[Math.round(Math.random() * (myarray.length - 1))]

to access a random value from the array.

访问数组中的随机值。

You can use thislink to see hot to generate a random number between a min and max number. (for your case min will always be 0).

您可以使用链接查看 hot 以生成最小和最大数字之间的随机数。(对于您的情况 min 将始终为 0)。

回答by Epsil0neR

Try this one: HTML:

试试这个:HTML:

<input type="button" click="randomize()" value="Click me" />

JS:

JS:

function randomize(){
  var myarray = new Array("item1", "item2", "item3");
  var randomId =  Math.floor((Math.random()*myarray.length));
  var randomItem = myarray[randomid];

  alert(randomItem);
}

回答by aquinas

http://jsfiddle.net/McKxp/

http://jsfiddle.net/McKxp/

JS:

JS:

var arr = ["foo","bar","baz"];

function getItem(){
    document.getElementById("something").innerHTML = arr[Math.floor(Math.random() * arr.length)];
}

HTML

HTML

<div id="something"></div>
<input type="button" onclick="getItem()" value="Click me"/>

回答by Gabriel Petrovay

Here is an example:

下面是一个例子:

http://jsfiddle.net/kUgHg/2/

http://jsfiddle.net/kUgHg/2/

The steps are:

步骤是:

  1. Create a button element and give it an ID:

    <button id="button1">Show random item</button>
    
  2. Get a reference to the button in your script:

    document.getElementById("button1");
    
  3. Compute a random index in your array based on the array length:

    Math.floor(Math.random() * array.length)
    
  4. Add a clickevent handler to your button that calls the random computation at step 3 and shows it a way you want:

    button.onclick = function() { … }
    
  1. 创建一个按钮元素并给它一个 ID:

    <button id="button1">Show random item</button>
    
  2. 获取对脚本中按钮的引用:

    document.getElementById("button1");
    
  3. 根据数组长度计算数组中的随机索引:

    Math.floor(Math.random() * array.length)
    
  4. click向您的按钮添加一个事件处理程序,该处理程序在步骤 3 中调用随机计算并以您想要的方式显示它:

    button.onclick = function() { … }
    

All in one:

一体:

var button = document.getElementById("button1");
var myarray = ["a", "b", "c", "d"];

button.onclick = function() {
    alert(myarray[Math.floor(Math.random() * myarray.length)]);
};