提升使用者體驗:點擊表格任一列,該列高亮並自動勾選該列的單選按鈕。只需幾行 jQuery 即可實現。
.selected { background: #FF6500; color: #fff; }
$(document).ready(function() {
$("#tablight tr:gt(0)").click(function() {
$(this).addClass("selected")
.siblings().removeClass("selected")
.end()
.find(":radio").prop("checked", true);
});
});
tr:gt(0):略過第 0 列(通常為標題列)。.siblings().removeClass(...):移除其他列高亮,確保單選視覺效果。.end():回到原本的 $(this) 繼續鏈式呼叫 .find(":radio")。.prop("checked", true):jQuery 1.6+ 建議用 prop 設定布林屬性。表格必須加上 id="tablight",每列包含一個 <input type="radio" name="...">。