2017-05-03 33 views
0

我在文本框中生成15位数字的JS代码,我想将生成的数字减少到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>

+0

检查此链接[小提琴](https://jsfiddle.net/gkzqqt6t/3/)。希望它有效。 –

回答

0

您可以用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>

+0

与此相关的问题是,最后三位数字与当前年份相关的几乎不会改变。 –

0

最有效的方式串它这样做将是从.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>

+0

谢谢Rory McCrossan。这是很好的帮助 – Joyson

+0

没问题。如果有帮助,不要忘记接受答案。 –

0

尝试用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>

+0

1. OP想要一个5位数字,而不是6. 2.与此相关的问题是,最后三位数字与当前年份相关的几乎不会改变。 –

+0

@RoryMcCrossan看到那里*'我想减少产生的数量到5或6' * .OP希望'5 r 6' – prasanth

+0

所以他们这样做,公平点。问题2仍然是一个因素 –

0

<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>

更新:产生更多的随机数

+1

问题在于最后三位数字与当前年份相关的变化很小。 –

+0

添加此行'randomNum = Math.round(Math.random()* randomNum);'@RoryMcCrossan –

0

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>

0

尝试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));