首頁  >  文章  >  web前端  >  一種jQuery日期範圍選擇器使用教學課程

一種jQuery日期範圍選擇器使用教學課程

小云云
小云云原創
2017-11-25 09:24:122470瀏覽

jQuery是一套跨瀏覽器的JavaScript函式庫,簡化HTML與JavaScript之間的操作。本文我們和大家分享一個jQuery日期範圍選擇器使用教學。

準備:

使用該日期選擇器外掛程式需要 jQuery 1.3.2+和Moment 2.2.0+的支援。

<link rel="stylesheet" href="css/daterangepicker.css" /><script src="js/moment.min.js"></script><script src="js/jquery.daterangepicker.js"></script>

HTML結構

在需要放置日期選擇器的地方加入下列html結構,就是一個輸入框。

<input type="text" id="datepicker" value="">

呼叫外掛

呼叫jQuery Date Range Picker外掛非常簡單,和其他常見的jQuery外掛一樣:

$(&#39;#datepicker&#39;).dateRangePicker(option);

設定參數

#該日期選擇器的預設組態參數如下:

{
    format: &#39;YYYY-MM-DD&#39;,
    separator: &#39; to &#39;,
    language: &#39;auto&#39;,
    startOfWeek: &#39;sunday&#39;,// or monday
    getValue: function()    {        return this.value;
    },
    setValue: function(s)    {        this.value = s;
    },
    startDate: false,
    endDate: false,
    minDays: 0,
    maxDays: 0,
    showShortcuts: true,
    time: {
        enabled: false
    },
    shortcuts:
    {        //&#39;prev-days&#39;: [1,3,5,7],
        &#39;next-days&#39;: [3,5,7],        //&#39;prev&#39; : [&#39;week&#39;,&#39;month&#39;,&#39;year&#39;],
        &#39;next&#39; : [&#39;week&#39;,&#39;month&#39;,&#39;year&#39;]
    },
    customShortcuts : [],
    inline:false,
    container: &#39;body&#39;,
    alwaysOpen:false,
    singleDate:false,
    batchMode:false,
    beforeShowDay: [function],    dayDivAttrs: [],    dayTdAttrs: [],    applyBtnClass: &#39;&#39;
}

format (String):Moment的日期格式。 點選這裡 查看Moment文件。

separator (String):日期字串之間的分隔符號。

language (String):預先定義的語言是"en"和"cn"。你可以使用這個參數自訂語言。也可以設定為"auto"讓瀏覽器自行偵測語言。

startOfWeek (String):"sunday" 或 "monday"。

getValue (Function):當從DOM元素取得日期範圍時會呼叫函數,函數的上下文被設定為datepicker DOM。

setValue (Function):當向DOM元素中寫入日期範圍時呼叫函數。

startDate (String or false):定義使用者允許的最早日期,格式和format相同。

endDate (String or false):定義使用者允許的最後日期,格式和format相同。

minDays (Number) :此參數定義日期範圍的最小天數,若設定為0,表示不限制最小天數。

maxDays (Number):此參數定義日期範圍的最大天數,若設定為0,表示不限制最大天數。

showShortcuts (Boolean) :先生或隱藏shortcuts區域。

time (Object):如果允許該參數就會新增時間的範圍選擇。

shortcuts (Object):定義快速鍵按鈕。

customShortcuts (Array):定義自訂快速鍵按鈕。

inline (Boolean):使用inline模式渲染該日期選擇器,而不是overlay模式。如果設定為true,則要一起設定container參數。

container (String, css selector || DOM Object) :要進行渲染的日期選擇器DOM元素。

alwaysOpen (Boolean):如果使用inline模式,你可能想要在頁面載入時就渲染日期選擇器。此參數設為true時會隱藏"close"按鈕。

singleDate (Boolean):設定為true可以選擇單一的日期。

batchMode (false / 'week' / 'month'):自動批次模式。

事件

當該日期選擇器在DOM中選擇某個日期範圍時會觸發三個事件。

$(&#39;#datepicker&#39;)
.dateRangePicker()
.bind(&#39;datepicker-change&#39;,function(event,obj){    console.log(obj);    // obj will be something like this:
    // {
    //      date1: (Date object of the earlier date),
    //      date2: (Date object of the later date),
    //      value: "2013-06-05 to 2013-06-07"
    // }})
.bind(&#39;datepicker-apply&#39;,function(event,obj){    console.log(obj);
})
.bind(&#39;datepicker-close&#39;,function(){    console.log(&#39;close&#39;);
});

API

在你呼叫$(dom).dateRangePicker()之後:

$(dom).data(&#39;dateRangePicker&#39;)
    .setDateRange(&#39;2013-11-20&#39;,&#39;2013-11-25&#39;);  //set date range, two date strings should follow the `format` in config object
    .clear();   // clear date range
    .close();   // close date range picker overlay
    .open();    // open date range picker overlay
    .destroy(); // destroy all date range picker related things

以上就是一種jQuery日期範圍選擇器使用教程,希望對大家有幫助。

相關建議:

php驗證一個日期的格式是否正確

Js取得目前日期時間及其它操作的方法

JavaScript取得目前日期和時間的方法詳解

以上是一種jQuery日期範圍選擇器使用教學課程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn