「jQuery」カレンダーから年と月うプルダウン表示にするサンプル

2022年1月28日

環境
以下のライブラリを利用します
・jquery.min.js
・jquery-ui.min.js
・jquery-ui.css
・jquery.ui.datepicker-ja.min.js
Datepicker Widgetについて
入力欄をクリックするとカレンダーが表示され、カレンダーの日付をクリックすると入力欄に日付が入力されます。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<p>年月日: <input type="text" id="regdate" /></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>
<script>
$(function() {
$.datepicker.setDefaults($.datepicker.regional["ja"]);
$( "#regdate" ).datepicker({
//年がプルダウン表示
changeYear: true,
//月がプルダウン表示
changeMonth: true,
// 現在から2年2ヶ月前
minDate: '-2y -2m',
// 現在から2年2ヶ月後
maxDate: '+2y +2m'
});
});
</script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> <p>年月日: <input type="text" id="regdate" /></p> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script> <script> $(function() { $.datepicker.setDefaults($.datepicker.regional["ja"]); $( "#regdate" ).datepicker({ //年がプルダウン表示 changeYear: true, //月がプルダウン表示 changeMonth: true, // 現在から2年2ヶ月前 minDate: '-2y -2m', // 現在から2年2ヶ月後 maxDate: '+2y +2m' }); }); </script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<p>年月日: <input type="text" id="regdate" /></p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>
<script>
      $(function() {
            $.datepicker.setDefaults($.datepicker.regional["ja"]);
            
        $( "#regdate" ).datepicker({
         //年がプルダウン表示
             changeYear: true, 
         //月がプルダウン表示
            changeMonth: true, 
          // 現在から2年2ヶ月前
              minDate: '-2y -2m',
          // 現在から2年2ヶ月後
              maxDate: '+2y +2m'
        });
      });
</script>

説明
changeYearにtrueを指定しています。年がプルダウン表示になります。
changeMonthにtrueを指定しています。月がプルダウン表示になります。
yは年でmは月でdは日です。
実行結果
カレンダーから年と月をプルダウン表示にします

jQuery

Posted by arkgame