简体   繁体   English

提交后如何显示随机数?

[英]How to display random number after submission?

I would like to ask how to display random number after submission? 我想问一下提交后如何显示随机数?

 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> 

Here you go. 干得好。 On the reload of the submitted form, the below scripts will change the button text. 在重新加载提交的表单时,以下脚本将更改按钮文本。

If you don't want to reload the page then you need to use libraries like jquery to submit the form using ajax . 如果您不想重新加载页面,则需要使用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> 

You could use Math.Random along with this as below, 您可以如下使用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> 

You need to make use of the preventDefault action on the submit button's click event to cancel the submit action, then calculate the random number and show that on the page. 您需要使用提交按钮的click事件上的preventDefault操作来取消提交操作,然后计算随机数并将其显示在页面上。

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

    <script>
    var stampmonths = [   "01","02","03","04","05","06","07","08","09","10","11","12"];

    var submit_button = document.getElementById('ref');
    function stopDefAction(evt) {
      evt.preventDefault();
      // now, since we cancelled the submit action, handle the action yourself
      // in this case, it's setting the submit button to show a random number
      var thedate = new Date();
      evt.target.value = stampmonths[thedate.getMonth()] + thedate.getDate() + thedate.getFullYear() +   thedate.getSeconds();

    }
    submit_button.addEventListener( 'click', stopDefAction, false );
    </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM