電商列表頁左側那排「品牌、價格區間、尺寸」的條件選單,看起來複雜,實際上的核心只有一句:每個維度的選擇結果存進隱藏欄位,最後組成一串查詢參數。以下示範這個骨架,並說明如何接上 ajax。
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="JquerySort.aspx.cs" Inherits="demo_JquerySort" %>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery 分類</title>
<script src="../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// 品牌
var alink01 = $("#linktype01").find("span");
alink01.click(function () {
alink01.each(function () {
$(this).removeClass("templinkactive").addClass("templink");
});
$(this).removeClass("templink").addClass("templinkactive");
$("#Brand").val($(this).attr("tag"));
});
// 價格
var alink02 = $("#linktype02").find("span");
alink02.click(function () {
alink02.each(function () {
$(this).removeClass("templinkactive").addClass("templink");
});
$(this).removeClass("templink").addClass("templinkactive");
$("#Price").val($(this).attr("tag"));
});
// 尺寸
var alink03 = $("#linktype03").find("span");
alink03.click(function () {
alink03.each(function () {
$(this).removeClass("templinkactive").addClass("templink");
});
$(this).removeClass("templink").addClass("templinkactive");
$("#Size").val($(this).attr("tag"));
SetPara();
});
});
function SetPara() {
var a = $("#Brand").val();
var b = $("#Price").val();
var c = $("#Size").val();
alert("1.aspx?a=" + a + "&b=" + b + "&c=" + c);
}
</script>
<style type="text/css">
.templinkactive {
padding: 5px;
text-decoration: none;
background-color: #2788DA;
color: #ffffff;
}
.templink {
cursor: pointer;
padding: 5px;
text-decoration: none;
}
table tr { height: 35px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr id="linktype01">
<td style="width:100px"><b>筆記本品牌</b></td>
<td><span class='templinkactive' tag="0">不限</span></td>
<td><span class='templink' tag="100101">聯想(Lenovo)</span></td>
<td><span class='templink' tag="100102">宏碁(Acer)</span></td>
<td><span class='templink' tag="100103">華碩(ASUS)</span></td>
<td><span class='templink' tag="100104">戴爾(DELL)</span></td>
<td><span class='templink' tag="100105">蘋果(Apple)</span></td>
<td><span class='templink' tag="100106">三星(SAMSUNG)</span></td>
</tr>
<tr id="linktype02">
<td style="width:100px"><b>價格范圍</b></td>
<td><span class='templinkactive' tag="0">不限</span></td>
<td><span class='templink' tag="100201">1000-2999</span></td>
<td><span class='templink' tag="100202">3000-3499</span></td>
<td><span class='templink' tag="100203">4000-4499</span></td>
<td><span class='templink' tag="100204">5000-5999</span></td>
<td><span class='templink' tag="100205">6000-6999</span></td>
<td><span class='templink' tag="100206">7000及以上</span></td>
</tr>
<tr id="linktype03">
<td style="width:100px"><b>尺寸范圍</b></td>
<td><span class='templinkactive' tag="0">不限</span></td>
<td><span class='templink' tag="100301">8.9英寸及以下</span></td>
<td><span class='templink' tag="100302">11英寸</span></td>
<td><span class='templink' tag="100303">12英寸</span></td>
<td><span class='templink' tag="100304">13英寸</span></td>
<td><span class='templink' tag="100305">14英寸</span></td>
<td><span class='templink' tag="100306">15英寸及以上</span></td>
<input type="hidden" id="Brand" value="0" />
<input type="hidden" id="Price" value="0" />
<input type="hidden" id="Size" value="0" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
linktype01/02/03),方便分別選取該列的 span。tag 上,畫面顯示的是「聯想(Lenovo)」這類人類可讀文字,程式取用的是 100101 這個代碼。templinkactive 加到自己身上,同時把值寫進對應的隱藏欄位。把 alert 換成實際請求即可,這也是原範例留給讀者的功課:
function SetPara() {
var params = {
brand: $("#Brand").val(),
price: $("#Price").val(),
size: $("#Size").val()
};
$.getJSON("1.aspx", params, function (data) {
$("#productList").html(data.html); // 後端回傳已組好的清單片段
});
}
用 $.getJSON/$.get 傳物件的好處是 jQuery 會自動處理 URL 編碼,不必自己拼字串——手動拼接時若值含 & 或中文就會出問題。
$(".filter-row").on("click", "span", handler) 搭配 data-field 標示要寫入哪個隱藏欄位,程式碼能砍掉三分之二。data-* 取代自訂的 tag 屬性。 tag 不是標準屬性,HTML 驗證不會通過;data-value 才是正解,且可用 $(this).data('value') 直接讀取。click 綁定對動態內容無效。location.search 或 history.pushState。