close

本文說明datepicker 事件 beforeShow beforeShowDay

其效果顯示日曆框前要做的事

一、基本JS/HTML語法

JS:

 $("#datepicker").datepicker({

           beforeShow : fun1,

           beforeShowDay : fun2

         });

二、基本說明

beforeShow : fun1

Default: null

function(Element imput,Object inst)

顯示日曆框前要做的事,對input 物件操作。

fun1為自訂方法。

function fun1(input, inst) {

  console.log("beforeShow");

  console.log("input",input);

  console.log("inst",inst);

}

beforeShowDay : fun2

Default: null

function (Date date)

顯示日曆框前要做的事,對日期操作。

fun2為自訂方法。

var array = [ "2016/01/14", "2016/01/15", "2016/01/16" ];

//本方法效果限制不可選的日期。(array中為不可選日期)

function fun2(date) {

  var string = jQuery.datepicker.formatDate('yy/mm/dd', date);

  console.log("beforeShowDay", string);

  return [ array.indexOf(string) == -1 ]

}

三、程式內容

<html>
<head>
<title>jquery.datepicker 23</title>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
  $(function() {
    //23-jQuery datepicker 事件 beforeShow beforeShowDay
    $("#datepicker").datepicker({
      //設定日期格式
      dateFormat : "yy/mm/dd",
      //顯示日曆框前要做的事,對input 物件操作
      beforeShow : fun1,
      //顯示日曆框前要做的事,對日期操作
      beforeShowDay : fun2
    });
    //function(Element imput,Object inst)
    //Element input 
    //Object datepicker
    function fun1(input, inst) {
  $("#showDateBox").append("beforeShow");
  $("#showDateBox").append("input"+input);
  $("#showDateBox").append("inst"+inst);
  console.log("beforeShow");
  console.log("input",input);
  console.log("inst",inst);
    }
    var array = [ "2016/01/14", "2016/01/15", "2016/01/16" ];
    //本方法效果限制不可選的日期。(array中為不可選日期)
    //function (Date date)
    function fun2(date) {
      var string = jQuery.datepicker.formatDate('yy/mm/dd', date);
      console.log("beforeShowDay", string);
      return [ array.indexOf(string) == -1 ]
    }
  });
</script>
</head>
<body>
  <div class="datetest">
    <p class="datag">
      Date: <input type="text" id="datepicker" value="">
    </p>
  </div>
  <div>說明:jQuery datepicker 事件 beforeShow beforeShowDay </div>
  <br>
  <div id="showDateBox"></div>
</body>
</html>
	

四、執行

圖1

五、測試

Date: 

說明:jQuery datepicker 事件 beforeShow beforeShowDay

相關參考:

其它參考:

jQuery教學目錄

其它文章



arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(0) 人氣()