文本框中的自动生成的数字

时间:2017-05-03 09:40:54

标签: javascript jquery asp.net json

我在文本框中生成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>
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:0)

您可以使用substr

对其进行子串

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

执行此操作的最有效方法是从.getTime()检索最后3位数字并将它们附加到2个随机整数。由于这些数字与当前日期的毫秒有关,因此它们应该表示生成的数字具有随机化的外观 - 尽管显然不是真正随机的。试试这个:

&#13;
&#13;
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;
}
&#13;
<form id="form1" runat="server">
  <div>
    <input type="text" tooltip="User Id" id="txt_usrid" />
  </div>
</form>
&#13;
&#13;
&#13;

答案 2 :(得分: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>

答案 3 :(得分: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>

已更新:生成更多随机数

答案 4 :(得分: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>

答案 5 :(得分:0)

尝试String.substring()方法:

&#13;
&#13;
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));
&#13;
&#13;
&#13;