我正在尝试创建一个程序,该程序使用javascript(在浏览器控制台中)在文本框中放入随机数/字母。我已经尽力做到了,但我做不到。 我最好的尝试是: 但不幸的是,它仅生成数字。有谁知道如何将两者随机化?如果有人可以帮助我,将不胜感激。 致谢,3hr3nw3rt 答案 0 :(得分:0) 您可以使用来生成字母数字字符串 var key = ((nums[Math.floor(Math.random() * nums.length)].toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()));
var key2 = ((Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()));
var key3 = ((Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()));
1 个答案:
function alphaNumericString(length) {
var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
console.log(alphaNumericString(3))