金子邦彦研究室プログラミングJavaScript による Web プログラミングDate Picker (jQuery と jQueryUI を使用)

Date Picker (jQuery と jQueryUI を使用)

jQuery と jQueryUI を使用して、Date Picker を作る.

◆ 動作画面の例

[image]

準備

◆ JavaScript に関する Web ブラウザの設定

jQueryUI を用いたDate Picker の例

◆ HTML ファイルの例

テーブルの先頭行と奇数行と偶数行で色を変えたいので、 jQuery のセレクタを使い、要素にクラス名を追加している

<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
    <head>

        <meta http-equiv="Content-Type" content="text/html; initial-scale=1.0; charset=Shift_JIS" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <meta http-equiv="Content-Script-Type" content="text/javascript" />
        <meta http-equiv="MSThemeCompatible" CONTENT="yes" />

        <title>
            Date Picker の例
        </title>
      
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery.ui.all.css" />
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> 

        <script type="text/javascript">
          <!--
            jQuery.noConflict();
            jQuery(document).ready(function($) {
              // Datepicker
              $('.datepicker').datepicker({
                inline: true
              });
            });
          //-->
        </script>

    </head>

    <body>
      <h1>Date Picker</h1>
            
            <!-- Datepicker -->
            <h2>Datepicker (inline)</h2>
            <div class="datepicker"></div>

    </body>
</html>

◆ 動作画面の例

[image]

jQueryUI を用いたDate Picker の例(日本語表示)

日本語表示したいので「 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1i18n/jquery.ui.datepicker-ja.min.js"></script>」を書き加えています.他の部分は変えていません。

◆ HTML ファイルの例

<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
    <head>

        <meta http-equiv="Content-Type" content="text/html; initial-scale=1.0; charset=Shift_JIS" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <meta http-equiv="Content-Script-Type" content="text/javascript" />
        <meta http-equiv="MSThemeCompatible" CONTENT="yes" />

        <title>
            Date Picker の例
        </title>
      
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery.ui.all.css" />
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> 
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>

        <script type="text/javascript">
          <!--
            jQuery.noConflict();
            jQuery(document).ready(function($) {
              // Datepicker
              $('.datepicker').datepicker({
                inline: true
              });
            });
          //-->
        </script>

    </head>

    <body>
      <h1>Date Picker</h1>
            
            <!-- Datepicker -->
            <h2>Datepicker (inline)</h2>
            <div class="datepicker"></div>

    </body>
</html>

◆ 動作画面の例

[image]

jQueryUI を用いたDate Picker の例(日本語表示 + showon button)

今度は input 要素で Date Picker を使う例です

◆ HTML ファイルの例

<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
    <head>

        <meta http-equiv="Content-Type" content="text/html; initial-scale=1.0; charset=Shift_JIS" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <meta http-equiv="Content-Script-Type" content="text/javascript" />
        <meta http-equiv="MSThemeCompatible" CONTENT="yes" />

        <title>
            Date Picker の例
        </title>
      
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery.ui.all.css" />
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> 
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>

        <script type="text/javascript">
      <!--
            jQuery.noConflict();
            jQuery(document).ready(function($) {
              // Datepicker
             $( '.jquery-ui-datepicker' ) . datepicker( {
                  showOn: "button",
                  buttonText: "カレンダーで選ぶ"
            } );
          } );
          // -->
    </script>
    </head>

    <body>
        <div>
        <p>日付: <input type="text" class="jquery-ui-datepicker"/></p>
        </div>

    </body>
</html>

[image]

              $(".jdatepicker").datepicker(
                {
                  dayNamesMin: ["日", "月", "火", "水", "木", "金", "土"],
                  monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
                  nextText: "次の月",
                  prevText: "前の月"
                }
              );