<span>
<tr>
<td>
<img onclick="addType()" src="${msUrl}/images/logo/add.png">產品範圍:
</td>
<td>
<select id="selectTypeOne" class="easyui-combobox" data-options="required:true">
<option>一級分類</option>
</select>
<select id="selectTypeTwo" class="easyui-combobox" data-options="required:true">
<option selected="selected">全部</option>
</select>
</td>
</tr>
<tr height="20px">
<td></td>
<td id="typeThree"></td>
</tr>
<tr height="30px">
<td></td>
<td>
<span id="typeOneResult"></span>
<span id="typeOneSubResult"></span>
<hr />
</td>
</tr>
</span>
// 第一級分類選擇
$('#selectTypeOne').combobox({
url: config.urlMap.typeList,
valueField: 'name',
textField: 'name',
required: true,
width: '100',
onSelect: function(row) {
typeName1 = row.name;
// 重置下級選單與顯示區
$('#typeThree').html("");
$('#typeOneResult').html("");
$('#typeOneSubResult').html("");
subTypeName = [];
// 動態載入第二級分類
$('#selectTypeTwo').combobox({
url: config.urlMap.typeList + "?parent=" + row.id,
valueField: 'name',
textField: 'name',
width: '100',
required: true,
onSelect: function(row) {
typeName2 = typeName1 + ">" + row.name;
// AJAX 載入第三級分類並渲染複選框
$.getJSON(config.urlMap.typeList + "?parent=" + row.id, function(data) {
var typeThreeName = "";
for (var i = 0; i < data.length; i++) {
typeThreeName += "<input onclick=clinkType('"
+ data[i].name + "') name='typeThree' value='"
+ data[i].name + "' type='checkbox'>"
+ data[i].name;
}
$('#typeThree').html(typeThreeName);
$('#typeOneResult').html(typeName2 + ">");
$('#typeOneSubResult').html("");
subTypeName = [];
});
}
});
}
});
// 方式一:手動位移(不推薦,效能較差)
Array.prototype.remove = function(dx) {
if (isNaN(dx) || dx > this.length) return false;
for (var i = 0, n = 0; i < this.length; i++) {
if (this[i] != this[dx]) {
this[n++] = this[i];
}
}
this.length -= 1;
};
// 方式二:使用 splice(推薦,原生支援)
Array.prototype.baoremove = function(dx) {
if (isNaN(dx) || dx > this.length) return false;
this.splice(dx, 1);
};
var subTypeName = [];
function clinkType(name) {
var index = subTypeName.indexOf(name);
if (index === -1) {
// 未選取 → 加入陣列
subTypeName.push(name);
} else {
// 已選取 → 移除
subTypeName.baoremove(index);
}
// 更新預覽顯示
$('#typeOneSubResult').html(subTypeName.join(","));
}
removeArrayItem(arr, index)$(document).on('click', 'input[name=typeThree]', handler)data[i].name 應經過 HTML 編碼再拼接select + fetch 降低依賴// API 回傳格式
[
{"id": 1, "name": "手機", "parent": 0},
{"id": 2, "name": "筆電", "parent": 0},
{"id": 3, "name": "iPhone", "parent": 1},
{"id": 4, "name": "Android", "parent": 1}
]