Home  >  Article  >  Web Front-end  >  How to use arrays for random roll call

How to use arrays for random roll call

一个新手
一个新手Original
2017-09-11 10:18:372007browse

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>随机点名简单版</title>
	<script>
	window.onload = function(){
		//0 - arr.length-1
		var arr = [&#39;宝宝&#39;,&#39;豆豆&#39;,&#39;慧新&#39;,&#39;洪红&#39;,&#39;黄黄&#39;,&#39;小子&#39;,&#39;杜度&#39;,&#39;方放&#39;,&#39;顾股&#39;,&#39;胡胡&#39;,&#39;谢写&#39;,&#39;蒋讲&#39;,&#39;陈美女&#39;];
		var name = document.getElementById(&#39;name&#39;);
		var btn = document.getElementById(&#39;namee&#39;);
		btn.onclick = function(){
			// 得到一个随机整数
			var idx = parseInt(Math.random()*arr.length);

			// 把取到的名字写入#name输入框
			name.value = arr[idx];
		}
	}
	
	</script>
</head>
<body>
	<input type="text" id="name">
	<button id="namee">随机点名</button>
</body>
</html>

The above is the detailed content of How to use arrays for random roll call. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn