parseInt() -JavaScriptの関数で整数を返す

parseInt()の説明
parseInt()関数は、文字を数値に変換します。
数値に変換できない場合は、NaNを返し(出力)ます。
■parseInt例
■ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 |
<form name="form" action="#"> <input type="text" name="m"> <input type="button" value="botan" onclick="botan()"> </form> <script> function botan(){ var whomach = form.m.value; whomach = parseInt(whomach); document.write(whomach); } </script> |
■ブラウザ表示
↓
500
■parseInt例
■ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<script type="text/javascript"> function quesu1(){ var whomach = form.m.value; whomach = parseInt(whomach); if(whomach >= 10){ document.write("財布に入ってた金額は10万円。あなたが申告した金額は、"+ whomach +"円でした。"); } } </script> <body> あなたが落とした財布の中身は幾ら!? <form name="form" action="#"> <input type="text" name="m"> <input type="button" value="quesu1" onclick="quesu1()"> </form> </body> |
■ブラウザ表示
あなたが落とした財布の中身は幾ら!?
↓
財布に入ってた金額は10万円。あなたが申告した金額は、500円でした。