`jquery.settime.js` 是一款輕量級時間選擇外掛,支援拖曳調整時/分/秒、鍵盤輸入、範圍限制、回調事件。本文說明基本引入、參數配置、進階選項與現代替代方案。
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="jquery.settime.js"></script>
<input type="text" id="timeInput">
<script>
$('#timeInput').setTime(); // 預設參數
</script>
| 參數 | 型別 | 預設 | 說明 |
|---|---|---|---|
| `drag` | Boolean | false | 是否啟用滑鼠拖曳調整數值 |
| `h` | Array | [0,23] | 小時範圍 [min, max] |
| `i` | Array | [0,59] | 分鐘範圍 |
| `s` | Array | [0,59] | 秒數範圍 |
| `a` | Number | 0.05 | 動畫加速度(拖曳慣性) |
| `constraint` | String | '' | 額外限制條件 |
| `starteffect` | Function | null | 動畫開始回調,參數為當前時間陣列 |
| `onmove` | Function | null | 拖曳中回調,參數 [當前值, 目標值] |
| `callback` | Function | null | 選擇完成回調 |
| `zindex` | Number/String | '0' | 彈出層 z-index |
| `cursor` | String | 'move' | 拖曳游標樣式 |
| `area` | Array | [[50,600],[10,5000]] | 拖曳可移動區域 [[xMin,xMax],[yMin,yMax]] |
$('#time2').setTime({
drag: true,
h: [5, 20], // 只能選 05:00 - 20:59
i: [0, 59],
s: [0, 59],
a: 0.05,
starteffect: function(d) { console.log('開始選擇:', d); },
onmove: function(e) { console.log('拖曳中:', e[0], '→', e[1]); },
callback: function() { console.log('確定選擇'); }
});
$('#time3').setTime({ drag: true, a: 0.05 }); // 簡易拖曳模式
// 取得值
$('#getBtn').click(function() {
alert('time1: ' + $('#time').val());
alert('time2: ' + $('#time2').val());
alert('time3: ' + $('#time3').val());
});
<input type="time" id="nativeTime" step="1" min="05:00" max="20:59">
<script>
document.getElementById('nativeTime').addEventListener('change', function() {
console.log('選擇時間:', this.value); // "HH:MM:SS"
});
</script>