TP5.1 + layui時間範圍設定方法
最近在製作一個專案時遇到一個問題,就是開始時間和結束時間是相關聯的,結束時間的選擇範圍是最小為開始時間,最大為開始時間90天后的時間,我這邊用的是layui的插件取得的日期,裡面有兩個參數min和max分別可以設定日期範圍;
其程式碼如下所示:
layui.use('laydate', function(){ var laydate = layui.laydate; //限定可选日期 var ins22 = laydate.render({ elem: '#test-limit1' ,min: '2016-10-14' ,max: '2080-10-14' }); });
但是怎麼取得max和min的值時,我遇到了難點,後面經過反覆研究Layui官方文檔和反覆的嘗試,終於解決了這一問題,分享給大家:
a.定義變數當前時間、最小時間、最大時間
var now = new Date(); var min = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + (now.getDate() + 1); var max = now.getFullYear() + "-" + (now.getMonth() + 2) + "-" + (now.getDate() + 2);
b.根據目前選擇的時間重新給max和min賦值
要取得max
和min
的值,首先要了解時間換算單位,大概思路為基本換算單位以小時為準,1小時為3600秒,因為laui中的時間戳是以毫秒為計算單位,所以需要再3600的基礎上再乘以1000,一天為24小時,總共90天所以是90*24*3600*1000
.
在選擇了開始時間後done : function (value, date)
中value的值為目前所選的時間,將該值轉換為時間戳記為:var date1 = new Date(value).getTime();
90天後的時間戳記為:var date2 = date1 90 * 24 * 3600 * 1000;
再將取得的時間轉換為年月日:
var date5 = { 'date': date3.getDate(), 'month': date3.getMonth() + 1, 'year': date3.getFullYear() };
將取得到的值賦值給結束時間的最小值和最大值:
end.config.max = date5; end.config.max.month = date5.month - 1; end.config.min = date; end.config.min.month = date.month - 1;
所以完整的程式碼為:
var start = laydate.render({ elem: '#bx_start', type: 'date', max: max, min: min, showBottom: false, btns: ['clear', 'confirm'], done: function (value, date) { var date1 = new Date(value).getTime();//将当前选择的时间转化为时间戳 var date2 = date1 + 90 * 24 * 3600 * 1000;//获取90天后的时间 var date3 = new Date(date2); var date5 = { 'date': date3.getDate(), 'month': date3.getMonth() + 1, 'year': date3.getFullYear() }; end.config.max = date5; end.config.max.month = date5.month - 1; end.config.min = date; end.config.min.month = date.month - 1; } });
c.將取得的min和max傳給結束時間
var end = laydate.render({ elem: '#bx_end', type: 'date', max: max, min: min, showBottom: false, done: function (value, date) { if ($.trim(value) == '') { var curDate = new Date(); date = { 'date': curDate.getDate(), 'month': curDate.getMonth() + 1, 'year': curDate.getFullYear() }; } start.config.max = date; start.config.max.month = date.month - 1; } });
以下為完整程式碼
<script> layui.use(['form','layedit', 'laydate','layer','element'], function() { $ = layui.jquery; layer = layui.layer; var form = layui.form; layedit = layui.layedit; laydate = layui.laydate; //定义变量当前时间、最小时间、最大时间 var now = new Date(); var min = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + (now.getDate() + 1); var max = now.getFullYear() + "-" + (now.getMonth() + 2) + "-" + (now.getDate() + 2); var start = laydate.render({ elem: '#bx_start', type: 'date', max: max, min: min, showBottom: false, btns: ['clear', 'confirm'], done: function (value, date) { var date1 = new Date(value).getTime();//将当前选择的时间转化为时间戳 var date2 = date1 + 90 * 24 * 3600 * 1000;//获取90天后的 var date3 = new Date(date2); var date5 = { &#39;date&#39;: date3.getDate(), &#39;month&#39;: date3.getMonth() + 1, &#39;year&#39;: date3.getFullYear() }; end.config.max = date5; end.config.max.month = date5.month - 1; end.config.min = date; end.config.min.month = date.month - 1; } }); var end = laydate.render({ elem: &#39;#bx_end&#39;, type: &#39;date&#39;, max: max, min: min, showBottom: false, done: function (value, date) { if ($.trim(value) == &#39;&#39;) { var curDate = new Date(); date = { &#39;date&#39;: curDate.getDate(), &#39;month&#39;: curDate.getMonth() + 1, &#39;year&#39;: curDate.getFullYear() }; } start.config.max = date; start.config.max.month = date.month - 1; } }); }); </script>
#相關文章推薦:《TP5.1 為圖片新增浮水印功能》《TP5.1 layui實作欄位數的呼叫》《最新的10個thinkphp影片教學》
以上是TP5.1 + layui時間範圍設定方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

layui 登入頁面跳轉設定步驟:新增跳轉代碼:在登入表單提交按鈕點選事件中新增判斷,成功登入後透過 window.location.href 跳到指定頁面。修改 form 配置:在 lay-filter="login" 的 form 元素中新增 hidden 輸入字段,name 為 "redirect",value 為目標頁面位址。

layui 提供了多種取得表單資料的方法,包括直接取得表單所有欄位資料、取得單一表單元素值、使用formAPI.getVal() 方法取得指定欄位值、將表單資料序列化並作為AJAX 請求參數,以及監聽表單提交事件獲取資料。

透過使用layui框架的響應式佈局功能,可以實現自適應佈局。步驟包括:引用layui框架。定義自適應佈局容器,設定layui-container類別。使用響應式斷點(xs/sm/md/lg)隱藏特定斷點下的元素。利用網格系統(layui-col-)指定元素寬度。透過偏移量(layui-offset-)建立間距。使用響應式實用工具(layui-invisible/show/block/inline)控制元素的可見性和顯示方式。

使用 layui 傳輸資料的方法如下:使用 Ajax:建立請求對象,設定請求參數(URL、方法、資料),處理回應。使用內建方法:使用 $.post、$.get、$.postJSON 或 $.getJSON 等內建方法簡化資料傳輸。

layui與Vue的差異主要體現在功能和關注點上。 layui專注於快速開發UI元素,提供預製元件簡化頁面建置;而Vue則是全端框架,注重資料綁定、元件化開發和狀態管理,更適合建構複雜應用程式。 layui學習簡單,適合快速建立頁面;Vue學習曲線陡峭,但有助於建立可擴展且易於維護的應用程式。根據專案需求和開發者技能水平,可以選擇合適的框架。

若要執行 layui,請執行以下步驟:1. 匯入 layui 腳本;2. 初始化 layui;3. 使用 layui 元件;4. 匯入 layui 樣式(可選);5. 確保腳本相容並注意其他注意事項。透過這些步驟,您就可以使用 layui 的強大功能來建立 web 應用程式。

layui是一個前端UI框架,它提供了豐富的UI元件、工具和功能,幫助開發人員快速建立現代化、響應式和互動式Web應用程序,特點包括:靈活輕量、模組化設計、豐富的元件、強大的工具和易於自訂。它廣泛應用於各種Web應用程式的開發中,包括管理系統、電商平台、內容管理系統、社交網路和行動裝置應用程式。

layui和vue是前端框架,layui是一種輕量級的函式庫,提供UI元件和工具;vue是一個全面的框架,提供UI元件、狀態管理、資料綁定和路由等功能。 layui基於模組化的架構,vue是基於組件化的架構。 layui擁有較小的生態系統,vue擁有龐大且活躍的生態系統。 layui學習曲線較低,vue學習曲線較陡。 layui適用於小型專案和快速開發UI元件,vue適用於大型專案和需要豐富功能的場景。
