jQuery UI 的 slider 元件是處理「範圍內選值」的簡便方案。本文示範一組實用的寫法:拖曳滑桿時即時更新輸入框的值,且兩者可以雙向同步。

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta charset="gb2312">
<title>滑桿範例</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<style>
.slider-box { background: #F0EFEB; font-family: Microsoft Yahei; padding-bottom: 10px; }
.slider-box .item { padding: 10px; }
.slider-box .item .tag,
.slider-box .item .slider,
.slider-box .item .val { float: left; margin-right: 18px; }
.slider-box .item .slider { width: 400px; }
.slider-box .item .slider div {
background: #8FBF0B;
border: none;
height: 0.5em;
margin-top: 0.5em;
}
.slider-box .item .slider div .ui-slider-handle {
background: #F4F3F1;
width: 1em;
height: 1em;
border-radius: 1em;
}
.slider-box .item .val input {
border: none;
border-bottom: 1px solid #ABADA8;
background: none;
padding: 0.1em 1em;
color: #E4531C;
font-weight: bold;
font-size: 1em;
width: 5em;
text-align: center;
}
.clr { clear: both; }
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<div class="slider-box">
<div class="item">
<div class="tag">騎行裡程:</div>
<div class="slider"><div id="budget"></div></div>
<div class="val"><input value="8800" name="budget" /></div>
</div>
<div class="clr"></div>
<div class="item">
<div class="tag">騎行天數:</div>
<div class="slider"><div id="days"></div></div>
<div class="val"><input value="9" name="days" /></div>
</div>
<div class="clr"></div>
</div>
<script>
$("#budget").slider({
min: 2000,
max: 17000,
step: 100,
value: $('input[name="budget"]').val(),
slide: function (event, ui) {
$('input[name="budget"]').val(ui.value);
}
});
$("#days").slider({
min: 1,
max: 20,
value: $('input[name="days"]').val(),
slide: function (event, ui) {
$('input[name="days"]').val(ui.value);
}
});
</script>
</body>
</html>
CSS 部分有兩個細節值得注意。.ui-slider-handle 是滑桿把手,用 border-radius: 50% 讓它變圓形。.slider div 選擇的是 jQuery UI 產生的填充條,設定背景色就能做出「已選範圍」的視覺效果。
上面的程式碼只做到「拖滑桿 → 改輸入框」。若要讓使用者直接在輸入框打字、滑桿也跟著動,需要監聽 change 事件:
$('input[name="budget"]').on('change', function () {
$("#budget").slider('value', this.value);
});
slider 的 value 方法既可用來取值,也可用來設值,這正是 jQuery UI 這類元件 API 的常見設計。
$("#budget").slider('value'); // 取得目前值
$("#budget").slider('values', 0); // 取得範圍型滑桿的第一個把手值
$("#budget").slider('option', 'max', 20000); // 動態改設定
$("#budget").slider('disable'); // 停用
$("#budget").slider('enable'); // 啟用
若需要「最小~最大」的區間選擇,建立時加上 range: true 並用 values 陣列指定兩個初始值,回呼中則改用 ui.values[0] 與 ui.values[1]。
如果不需要支援舊瀏覽器,原生 <input type="range"> 已經能完成大部分需求,不必引入 jQuery:
<input type="range" id="budget" min="2000" max="17000" step="100" value="8800">
<script>
document.getElementById('budget').addEventListener('input', function () {
document.querySelector('input[name="budget"]').value = this.value;
});
</script>
原生 range 的優點是零依賴、可鍵盤操作、自帶無障礙支援;缺點是樣式客製需要寫 ::-webkit-slider-thumb 等廠商前綴偽元素。若只是要簡單的單值選擇,原生方案更合適;若需要範圍選擇、刻度標籤、自訂把手外觀,jQuery UI 或 newer 的套件(如 noUiSlider)會更省事。