DIV CSS 佈局教程網

jQuery 手機端日期與時間選擇器
編輯:JQuery常見問題     

效果說明

點一下文字欄位,畫面下方會推出一個滾動式面板,上下拖動選年、月、日與時間,按下確定後才把值寫回欄位。這種「輪盤式」輸入在行動裝置上比系統原生的日期輸入更符合亞洲使用者的操作習慣,也不容易被行動鍵盤遮住。

效果體驗:https:///keleyi/phtml/yidong/1.htm(請使用支援 HTML5 的瀏覽器)

需要載入的檔案

<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="email=no">

<link href="common.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.11.1.min.js"></script>
<script src="date.js"></script>
<script src="iscroll.js"></script>

其中 date.js 是選擇器主體,iscroll.js 負責面板內的慣性滾動。viewport 的設定避免行動裝置自動放大;兩個 format-detection 則是關掉 iOS 對數字串的錯誤辨識,否則日期裡的數字可能被自動變成電話連結。

呼叫方式

$(function(){
  $('#beginTime').date();                          // 只選日期
  $('#endTime').date({ theme : "datetime" });     // 日期 + 時間
});

透過參數就能切換模式,同一頁可以同時放純日期與日期時間兩種欄位。

頁面結構

<div class="demo">
 <div class="lie">日期設置-1:<input id="beginTime" class="kbtn" /></div>
 <div class="lie">日期設置-2:<input id="endTime" class="kbtn" /></div>
</div>
<div id="datePlugin"></div>

實務提醒

  • 純前端的日期選擇器不應作為唯一把關,區間是否合法(起始日晚於結束日)務必在後端再驗一次。
  • 行動裝置的軟鍵盤與瀏覽器工具列會影響可用高度,面板高度建議以百分比或 dvh 單位安排。
  • 若只需單純選日期,HTML5 原生的 <input type="date"> 已能觸發系統 picker,可作為輕量替代方案。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved