フォームの中でプルダウンメニューにて「年・月・日」を選択できるようにしたいのですが
デフォルトは常に当日の日付が表示されるようにしたいと思っております。
どのようにすればいいのでしょうか?
また、別のプルダウンの項目をサーバーにアップしたCSVから引っ張ってくるにはどうしたらいいでしょうか?
JavaScriptは初心者で検討がつきませんのでなるべくわかり易く教えていただけると助かります。
宜しくお願いいたします。
ベタですが、こんな感じでどうでしょう。
月の大小、うるう年は考慮していません。
<html> <head> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form> <script type="text/javascript"> <!-- today = new Date(); year = today.getFullYear(); month = today.getMonth() + 1; day = today.getDate(); //年 document.write('<select>'); for (i = year - 2; i <= year + 2; i++) { selected = (i == year) ? 'selected' : ''; document.write('<option ' + selected + '>' + i + '</option>'); } document.write('</select>年'); //月 document.write('<select>'); for (i = 1; i <= 12; i++) { selected = (i == month) ? 'selected' : ''; document.write('<option ' + selected + '>' + i + '</option>'); } document.write('</select>月'); //日 document.write('<select>'); for (i = 1; i <= 31; i++) { selected = (i == day) ? 'selected' : ''; document.write('<option ' + selected + '>' + i + '</option>'); } document.write('</select>日'); --> </script> </form> </body> </html>
参考「Dateオブジェクト」
回答者 | 回答 | 受取 | ベストアンサー | 回答時間 | |
---|---|---|---|---|---|
1 | Cherenkov | 1504回 | 1288回 | 493回 | 2011-01-06 05:18:17 |
回答ありがとうございます。
うまく表示できました。
見ている方の為に補足ですが
最初の2行のhtmlとheadの開始タグがダブっているので要りませんね。
引き続きプルダウンの項目をCSVから引っ張ってくる方法のご回答も宜しくお願いいたします。
追記です。
具体的に何がしたいのかというと
送信者という項目でCSVに追加した「人名リスト」から選択する。
もしくはテキストボックスに入力した名前を送信するといったことをしたいのです。
宜しくお願いします。