提交后如何显示随机数?

时间:2016-11-09 01:15:09

标签: javascript html random

我想询问提交后如何显示随机数?

var stampmonths = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
var thedate = new Date();
document.getElementById('ref').value = document.write(
  stampmonths[thedate.getMonth()] +
  thedate.getDate() +
  thedate.getFullYear() +
  thedate.getSeconds()
);
<form>
  <input type="submit" id="ref" name="submit" value="submit" />
</form>

2 个答案:

答案 0 :(得分:0)

你走了。在重新加载提交的表单时,以下脚本将更改按钮文本。

如果您不想重新加载页面,则需要使用jquery等库来使用ajax提交表单。

<!DOCTYPE html>
 <html>
<head>
</head>
<body>
<form>
<input type="submit" id="ref" name="submit" value="submit" />
</form>
</body>

<script>
var stampmonths = new Array(   "01","02","03","04","05","06","07","08","09","10","11","12");

var thedate = new Date();
document.getElementById('ref').value = stampmonths[thedate.getMonth()] + thedate.getDate() + thedate.getFullYear() +   thedate.getSeconds();
</script>
</html>

您可以使用Math.Random,如下所示,

<!DOCTYPE html>
 <html>
    <head>
    </head>
    <body>
    <form>
    <input type="submit" id="ref" name="submit" value="submit" />
    </form>
    </body>

    <script>
    var stampmonths = new Array(   "01","02","03","04","05","06","07","08","09","10","11","12");
    var rnd = Math.floor((Math.random() * 9) + 1);;
    var thedate = new Date();
    document.getElementById('ref').value = rnd.toString() + stampmonths[thedate.getMonth()] + thedate.getDate() + thedate.getFullYear() +   thedate.getSeconds();
    </script>
    </html>

答案 1 :(得分:0)

您需要使用提交按钮的点击事件中的preventDefault action取消提交操作,然后计算随机数并在页面上显示。

company_business_name