やりたいこと
2017/05
を基準にした、今月までの
年/月
がセットのプルダウンを作成したいです
参考サイトより
js
1<script> 2(function() { 3 'use strict'; 4 5 /* 6 今日の日付データを変数todayに格納 7 */ 8 var optionLoop, this_day, this_month, this_year, today; 9 today = new Date(); 10 this_year = today.getFullYear(); 11 this_month = today.getMonth() + 1; 12 this_day = today.getDate(); 13 14 /* 15 ループ処理(スタート数字、終了数字、表示id名、デフォルト数字) 16 */ 17 optionLoop = function(start, end, id, this_day) { 18 var i, opt; 19 20 opt = null; 21 for (i = start; i <= end ; i++) { 22 if (i === this_day) { 23 opt += "<option value='" + i + "' selected>" + i + "</option>"; 24 } else { 25 opt += "<option value='" + i + "'>" + i + "</option>"; 26 } 27 } 28 return document.getElementById(id).innerHTML = opt; 29 }; 30 31 32 /* 33 関数設定(スタート数字[必須]、終了数字[必須]、表示id名[省略可能]、デフォルト数字[省略可能]) 34 */ 35 optionLoop(1950, this_year, 'id_year', this_year); 36 optionLoop(1, 12, 'id_month', this_month); 37 optionLoop(1, 31, 'id_day', this_day); 38})();
この処理にて
optionLoopの年を1950から2017に変更すれば
選択できる月の変更は可能なのはわかるのですが
optionLoopの年と月を使い
年/月
が一つになった
プルダウンを作成したいのですがうまくいきません。
どうすれば実現可能でしょうか?
回答2件
あなたの回答
tips
プレビュー