星級評分是電商、內容社區、產品反饋中的核心交互組件。本文實現一個輕量級 jQuery 評分插件,支持單擊選星、雙擊清空、多維度並列展示、鍵盤無障礙訪問。
<div class="rating-system">
<!-- 維度 1: 功能 -->
<div class="rating-dimension">
<div class="dim-title">功能:</div>
<div class="dim-desc">你覺得這個創意在功能上優秀嗎?</div>
<ul class="star-group" title="雙擊取消評分" data-dim="function">
<li data-value="1">1</li>
<li data-value="2">2</li>
3
4
5
</ul>
<input type="hidden" name="rating_function" class="rating-value">
</div>
<!-- 維度 2: 外觀 -->
<div class="rating-dimension">
<div class="dim-title">外觀:</div>
<div class="dim-desc">你覺得這個創意在外觀上優秀嗎?</div>
<ul class="star-group" title="雙擊取消評分" data-dim="appearance">
<li data-value="1">1</li> ... <li data-value="5">5</li>
</ul>
<input type="hidden" name="rating_appearance" class="rating-value">
</div>
<!-- 更多維度... -->
</div>
.rating-system { width: 500px; margin: 0 auto; padding: 15px 0; font-family: "Microsoft YaHei"; }
.rating-dimension { float: left; width: 500px; padding-right: 20px; margin-bottom: 15px; }
.dim-title { font-weight: bold; font-size: 1.2em; padding: 4px 0; }
.dim-desc { line-height: 25px; color: #666; }
.star-group { margin: 10px 0; list-style: none; }
.star-group li {
float: left; width: 98px; height: 30px; line-height: 30px;
text-align: center; border: 1px solid #ddd; background: #f1f1f1;
cursor: pointer; transition: all 0.2s; user-select: none;
}
.star-group li:hover { background: #e0e0e0; }
.star-group li.active {
background: #308A95; color: #fff; border-color: #308A95;
}
.clearfix { clear: both; }
$(function() {
// 1. 單擊選星:激活當前及之前所有星星
$('.star-group li').click(function() {
var $this = $(this);
var $group = $this.closest('.star-group');
var value = $this.data('value');
// 更新 UI
$this.addClass('active').prevAll().addClass('active');
$this.nextAll().removeClass('active');
// 更新隱藏域值 (表單提交用)
$group.siblings('.rating-value').val(value);
// 觸發自定義事件 (供業務邏輯擴展)
$group.trigger('rating.change', [value, $group.data('dim')]);
});
// 2. 雙擊清空評分
$('.star-group li').dblclick(function() {
var $group = $(this).closest('.star-group');
$group.find('li').removeClass('active');
$group.siblings('.rating-value').val('');
$group.trigger('rating.clear', [$group.data('dim')]);
});
// 3. 鍵盤無障礙支持 (Tab 聚焦、方向鍵選星、Enter 確認)
$('.star-group li').on('keydown', function(e) {
var $items = $(this).closest('.star-group').find('li');
var index = $items.index(this);
switch(e.key) {
case 'ArrowRight':
case 'ArrowDown':
e.preventDefault();
$items.eq(Math.min(index+1, 4)).focus().click();
break;
case 'ArrowLeft':
case 'ArrowUp':
e.preventDefault();
$items.eq(Math.max(index-1, 0)).focus().click();
break;
case 'Enter':
case ' ':
e.preventDefault();
$(this).click();
break;
case 'Escape':
$items.removeClass('active');
$group.siblings('.rating-value').val('');
break;
}
});
// 4. 表單提交前驗證 (可選)
$('form').submit(function() {
var valid = true;
$('.rating-value').each(function() {
if (!$(this).val()) {
alert('請完成所有維度評分');
valid = false;
return false;
}
});
return valid;
});
});
// CSS: 寬度 50% 顯示半星,使用背景圖片或 clip-path
// JS: 計算鼠標在 li 中的相對位置,data-value 存 0.5 步長
function renderRating($group, value) {
$group.find('li').each(function(i) {
$(this).toggleClass('active', i < value);
}).off('click dblclick keydown').css('cursor', 'default');
}
$('.star-group').on('rating.change', function(e, value, dim) {
$.post('/api/rate', {
targetId: window.TARGET_ID,
dimension: dim,
score: value
});
});
// 後端模板輸出
<ul class="star-group" data-dim="function">
<li data-value="1" class="<%= rating.function >= 1 ? 'active' : '' %>">1</li>
...
</ul>
<input type="hidden" name="rating_function" value="<%= rating.function %>">
參考原文提供的完整 HTML/CSS/JS 代碼,可直接保存為 .html 文件運行。
<ul><li> 而非 <div>,支持鍵盤導航input 存儲值,表單原生提交無需額外 JS 收集rating.change,業務邏輯獨立綁定