【问题标题】:Auto Generated number in textbox文本框中自动生成的数字
【发布时间】:2017-05-03 09:40:53
【问题描述】:

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

【问题讨论】:

  • 查看此链接fiddle。希望它有效。

标签: javascript jquery asp.net json


【解决方案1】:

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

【讨论】:

  • 1. OP 想要一个 5 位数字,而不是 6。2. 问题是最后三位数字几乎不会改变,因为它们与当前年份相关。
  • @RoryMcCrossan 看到那里 I want to reduce the number generated to 5 or 6 .OP 想要5 r 6
【解决方案2】:

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

【讨论】:

  • 这个问题是最后三位数字几乎不会改变,因为它们与当前年份相关。
【解决方案3】:

最有效的方法是从 .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>

【讨论】:

  • 感谢罗里·麦克罗桑。这是很好的帮助
  • 没问题。如果有帮助,请不要忘记接受答案。
【解决方案4】:

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

更新:生成更多随机数

【讨论】:

  • 这个问题是最后三位数字几乎不会改变,因为它们与当前年份相关。
  • 添加这一行randomNum = Math.round(Math.random() * randomNum);@RoryMcCrossan
【解决方案5】:

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>

【讨论】:

    【解决方案6】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多