i JS代码,该代码在文本框中生成15位数字,我想将生成的数字减少到5或6位。请帮助我。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime();
window.onload = function () {
document.getElementById("txt_usrid").value = randomNum;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid"/>
</div>
</form>
</body>
</html>
尝试使用Array#slice
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime().toString().slice(-4);
window.onload = function() {
document.getElementById("txt_usrid").value = randomNum;
}
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid" />
</div>
</form>
您可以用substr
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime();
window.onload = function () {
document.getElementById("txt_usrid").value = randomNum.substr(0,5); // HERE ;)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid"/>
</div>
</form>
</body>
</html>
做到这一点的最有效方法是从.getTime()
检索最后3个数字,并将它们附加到2个随机整数。由于这些数字与当前日期的毫秒相关,它们应该意味着生成的数字的外观是随机化的 - 尽管显然不是真正的随机数。尝试以下操作:
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime().toString().slice(-3);
window.onload = function() {
document.getElementById("txt_usrid").value = randomNum;
}
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid" />
</div>
</form>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(now.getTime() / 1000000000); // it was creating the number of milliseconds since 1970/01/01 so it should be less
randomNum = Math.round(Math.random() * randomNum);
window.onload = function () {
document.getElementById("txt_usrid").value = randomNum;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid"/>
</div>
</form>
</body>
</html>
更新:生成更多随机数
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime().toString().slice(-3);
window.onload = function() {
document.getElementById("txt_usrid").value = randomNum;
}
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid" />
</div>
</form>
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime().toString().slice(-3);
window.onload = function() {
document.getElementById("txt_usrid").value = randomNum;
}
<form id="form1" runat="server">
<div>
<input type="text" tooltip="User Id" id="txt_usrid" />
</div>
</form>
try string.substring((方法:
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime();
console.log(randomNum.substring(5, 11));