2016-11-09 6 views
0

提出後に乱数を表示する方法を尋ねますか?あなたが行くここ投稿後に乱数を表示するには?

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>

+0

質問している内容が明確ではありません。送信ボタンの値を置き換える方法や、送信ボタンを押した後に乱数だけを表示する方法を尋ねていますか? –

答えて

0

。送信されたフォームのリロード時に、以下のスクリプトはボタンテキストを変更します。

ページを再読み込みしない場合は、を使用してフォームを送信するには、jqueryのようなライブラリを使用する必要があります。

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

あなたは、あなたが送信ボタンのクリックイベントにpreventDefault actionを利用するために必要な、以下のようにこれに伴って

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

0

Math.Randomを使用することができます送信アクションをキャンセルしてからthを計算するe乱数とそのページに表示されます。

<!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> 
関連する問題