<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
//16进制(数值+字母)
function random1(len) {
var pwd = ""
for(var idx = 0 idx < len idx ++){
pwd = pwd + ((Math.random() * 16) & (0x5 | 0x9)).toString(16)
}
return pwd
}
//ASCII (数值+字母+特殊字符)
function random2(len){
//ASCII 33 ~ 126
var pwd = ""
for(var idx = 0 idx < len idx ++){
pwd = pwd + String.fromCharCode(parseInt(Math.random() * 93 + 33))
}
return pwd
}
//纯数字
function random3(len){
var pwd = ""
for(var idx = 0 idx < len idx ++){
var seed = parseInt(Math.random() * 9)
pwd = pwd + seed
}
return pwd
}
function create(){
var result = document.getElementById("result1")
for(var i = 0 i < 10 i++){
result.innerText = result.innerText + random1(6) + "\n"
}
var result = document.getElementById("result2")
for(var i = 0 i < 10 i++){
result.innerText = result.innerText + random2(6) + "\n"
}
var result = document.getElementById("result3")
for(var i = 0 i < 10 i++){
result.innerText = result.innerText + random3(6) + "\n"
}
}
</script>
</head>
<body>
<div id="result1" style="border:1px solid #e0e0e0width: 200px height: 300pxmargin-top: 10pxfloat: left">
</div>
<div id="result2" style="border:1px solid #e0e0e0width: 200px height: 300pxmargin-top: 10pxmargin-left: 20pxfloat: left">
</div>
<div id="result3" style="border:1px solid #e0e0e0width: 200px height: 300pxmargin-top: 10pxmargin-left: 20pxfloat: left">
</div>
<button onclick="create()" style="padding: 3px 5pxmargin: 5px 20px">Random</button>
</body>
</html>
给你提供个思路:
创建一个6位的数组
循环数组,6次随机获取a-z,A-Z,0-9的字符和数字
将随机获取的字符和数字填入相对应数组下标之中
将数组的值拼接转换成字符串,填入文本框
谢谢采纳!