在預約、報名或排班等情境中,常需要讓使用者從一組時間段中挑選。本文以 jQuery 為基礎,示範如何把一段 JSON 資料渲染成可點選的時間段方塊,並針對「已過期」與「已選滿」的狀態給出滑鼠移入提示。
先定義一段 JSON,其中 time 代表時間段,status 代表狀態:1 表示可選擇,2 表示已過期,3 表示已選滿。
var mcode = {
"minfo": [
{"time":"9:00-10:00","status":2},
{"time":"10:00-11:00","status":1},
{"time":"11:00-12:00","status":3},
{"time":"13:00-14:00","status":1},
{"time":"14:00-15:00","status":1},
{"time":"15:00-16:00","status":1},
{"time":"16:00-17:00","status":1},
{"time":"17:00-18:00","status":1}
]
};
使用 for...in 迴圈即可逐一取出每個時間段與其狀態:
for (var i in mcode.minfo) {
mcode.minfo[i].time + mcode.minfo[i].status;
}
根據狀態為每個時間段加上不同的 class,並在不可選擇的項目中補上提示文字與小箭頭:
var html = '';
for (var i in mcode.minfo) {
if (mcode.minfo[i].status === 2) {
html += '<span class="unspan1 ';
} else if (mcode.minfo[i].status === 3) {
html += '<span class="unspan2 ';
} else {
html += '<span class=" ';
}
if ((i + 1) % 3 === 0) {
html += '" >';
} else {
html += 'mspan" >';
}
html += mcode.minfo[i].time;
if (mcode.minfo[i].status === 2) {
html += '<label>已過期</label>';
} else if (mcode.minfo[i].status === 3) {
html += '<label>已選滿</label>';
}
if (mcode.minfo[i].status !== 1) {
html += '<em></em>';
}
html += "</span>";
}
$("#test").empty().html(html);
可選擇的項目在點擊後回傳選擇結果;已過期或已選滿的項目則在滑鼠移入時顯示提示,移開後自動隱藏。
$.each($("#test span"), function (k, v) {
if ($(this).hasClass("unspan1") || $(this).hasClass("unspan2")) {
$(this).hover(function () {
$(this).find("label").css({"display":"block"});
$(this).find("em").css({"display":"block"});
}, function () {
$(this).find("label").css({"display":"none"});
$(this).find("em").css({"display":"none"});
});
} else {
$(this).click(function () {
$("#result").empty().html("您選擇了:" + $(this).text());
});
}
});
可選擇的時間段以橘黃色呈現,已過期為灰藍色、已選滿為淡紅色,讓使用者一眼就能分辨狀態。
#test { width: 430px; padding: 35px; border: 1px solid #666666; overflow: hidden; margin: 100px auto 0px; }
#test span { display: block; background: #FF6600; width: 130px; height: 30px; line-height: 30px; text-align: center; float: left; _display: inline; position: relative; margin-bottom: 15px; cursor: pointer; }
#test .mspan { margin-right: 20px; }
#test .unspan1 { background: #D2E0E6; cursor: default; }
#test .unspan2 { background: #ffcaca; cursor: default; }
#test label { position: absolute; left: 25px; top: -18px; width: 60px; line-height: 20px; background: #F3F3F3; padding: 1px 10px; border: 1px solid #CCCCCC; display: none; }
#test em { display: block; border-color: #F3F3F3 transparent transparent transparent; border-style: solid; border-width: 6px; padding: 0; width: 0; height: 0; font-size: 0; line-height: 0; position: absolute; left: 58px; top: 5px; display: none; }
#result { margin: 10px auto 0px; text-align: center; }
把上述資料、迴圈、事件與樣式整合,即可得到一個可直接運行的頁面:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="https://demo.jb51.net/jslib/jquery/jquery.js"></script>
<style type="text/css">
*{margin:0px;padding:0px;}
#test{ width:430px; padding:35px; border:1px solid #666666; overflow:hidden; margin:100px auto 0px;}
#test span{display:block; background:#FF6600; width:130px; height:30px; line-height:30px; text-align:center; float:left; _display:inline; position:relative; margin-bottom:15px; cursor:pointer;}
#test .mspan{margin-right:20px;}
#test .unspan1{background:#D2E0E6; cursor:default}
#test .unspan2{background:#ffcaca; cursor:default;}
#test label{position:absolute; left:25px; top:-18px; width:60px; line-height:20px; background:#F3F3F3; padding:1px 10px; border:1px solid #CCCCCC; display:none;}
#test em{display:block; border-color:#F3F3F3 transparent transparent transparent; border-style:solid; border-width:6px; padding:0; width:0; height:0; font-size:0; line-height:0; position:absolute; left:58px; top:5px; display:none;}
#result{ margin:10px auto 0px; text-align:center}
</style>
</head>
<body>
<div id="test"></div>
<div id="result"></div>
<script type="text/javascript">
var mcode = {"minfo":[
{"time":"9:00-10:00","status":2},
{"time":"10:00-11:00","status":1},
{"time":"11:00-12:00","status":3},
{"time":"13:00-14:00","status":1},
{"time":"14:00-15:00","status":1},
{"time":"15:00-16:00","status":1},
{"time":"16:00-17:00","status":1},
{"time":"17:00-18:00","status":1}
]};
var html = '';
for (var i in mcode.minfo) {
if (mcode.minfo[i].status === 2) { html += '<span class="unspan1 '; }
else if (mcode.minfo[i].status === 3) { html += '<span class="unspan2 '; }
else { html += '<span class=" '; }
if ((i + 1) % 3 === 0) { html += '" >'; } else { html += 'mspan" >'; }
html += mcode.minfo[i].time;
if (mcode.minfo[i].status === 2) { html += '<label>已過期</label>'; }
else if (mcode.minfo[i].status === 3) { html += '<label>已選滿</label>'; }
if (mcode.minfo[i].status !== 1) { html += '<em></em>'; }
html += "</span>";
}
$("#test").empty().html(html);
$.each($("#test span"), function (k, v) {
if ($(this).hasClass("unspan1") || $(this).hasClass("unspan2")) {
$(this).hover(function () {
$(this).find("label").css({"display":"block"});
$(this).find("em").css({"display":"block"});
}, function () {
$(this).find("label").css({"display":"none"});
$(this).find("em").css({"display":"none"});
});
} else {
$(this).click(function () {
$("#result").empty().html("您選擇了:" + $(this).text());
});
}
});
</script>
</body>
</html>
如此一來,一個具備狀態區分與互動提示的時間段選擇元件就完成了,可依實際需求替換資料來源與配色。