勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

jQueryの「DateTimePicker」プラグインで日付や時間選択時に指定したイベントを実行させる方法

  jQuery JavaScript

jQueryの「DateTimePicker」プラグインで日付や時間選択時、あらかじめ指定した処理を実行させたい。以下に対応方法をメモ。

 

DateTimePicker

公式サイト

https://xdsoft.net/jqplugins/datetimepicker/

GitHub

https://github.com/xdan/datetimepicker

CDN

https://cdnjs.com/libraries/jquery-datetimepicker

https://www.jsdelivr.com/package/npm/jquery-datetime-picker

 

対応方法

ソースコード

<input type="text" id="date">

<script type="text/javascript" src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src=" https://cdn.jsdelivr.net/npm/jquery-datetime-picker@2.5.11/build/jquery.datetimepicker.full.min.js "></script>
<link href=" https://cdn.jsdelivr.net/npm/jquery-datetime-picker@2.5.11/jquery.datetimepicker.min.css " rel="stylesheet">

<script>
$.datetimepicker.setLocale(`ja`);
$(`#date`).datetimepicker({
    format: `Y-m-d H:i:s`,
    //日付選択時の処理
    onSelectDate:function(date, obj){
        console.log(`日付が選択されました。`);
    },
    //時間選択時の処理
    onSelectTime: function(time, obj){
        console.log(`時間が選択されました。`);
    }
});
</script>

「onSelectDate:function」のdateやtimeは選択した日時のオブジェクトが格納される。

 - jQuery JavaScript

  関連記事

jQueryにて複数のajax処理のレスポンスをまとめて表示する方法

同時にajax処理を行う際、挙動やコードの書き方に悩まされることが多い。例えばA ...

tableタグでスクロール時にヘッダーを残して表示する方法(jquery-decapitate)

テーブルタグ内で縦にスクロールした際にテーブルヘッダー部分は表示させたままテーブ ...

jQueryで指定した要素を簡単にフォーカスするfocusbleの使い方

divやspanなど指定した要素を簡単にフォーカスするjQueryプラグインがあ ...

「jQuery.browser is undefined」エラーの対応方法

jQueryのプラグインを使っていて1.9以降だと「jQuery.browser ...

jQueryで特定のdata属性をセレクタとして指定する方法

jQueryで特定のdata属性をクリックした際に何らかのイベントを実行したかっ ...