Select 的操作雖然不難,但每次都要寫 get(0).options[i] 這種長句還是挺惱人。本文把它包成一個外掛,讓取值、設值、增刪、清空都能用一行語法完成。
// 項目數量
jQuery.fn.size = function () {
return jQuery(this).get(0).options.length;
};
// 選中項的索引
jQuery.fn.getSelectedIndex = function () {
return jQuery(this).get(0).selectedIndex;
};
// 選中項的文字
jQuery.fn.getSelectedText = function () {
if (this.size() == 0) return "下拉框中無選項";
var index = this.getSelectedIndex();
return jQuery(this).get(0).options[index].text;
};
// 選中項的值
jQuery.fn.getSelectedValue = function () {
if (this.size() == 0) return "下拉框中無選中值";
return jQuery(this).val();
};
// 以值設為選中
jQuery.fn.setSelectedValue = function (value) {
jQuery(this).get(0).value = value;
};
// 以文字設為選中(比對第一個符合項)
jQuery.fn.setSelectedText = function (text) {
var isExist = false;
var count = this.size();
for (var i = 0; i < count; i++) {
if (jQuery(this).get(0).options[i].text == text) {
jQuery(this).get(0).options[i].selected = true;
isExist = true;
break;
}
}
if (!isExist) alert("下拉框中不存在該項");
};
// 以索引設為選中
jQuery.fn.setSelectedIndex = function (index) {
var count = this.size();
if (index >= count || index < 0) {
alert("選中項索引超出範圍");
} else {
jQuery(this).get(0).selectedIndex = index;
}
};
// 判斷是否存在指定值
jQuery.fn.isExistItem = function (value) {
var count = this.size();
for (var i = 0; i < count; i++) {
if (jQuery(this).get(0).options[i].value == value) return true;
}
return false;
};
// 新增一項
jQuery.fn.addOption = function (text, value) {
if (this.isExistItem(value)) {
alert("待添加項的值已存在");
} else {
jQuery(this).get(0).options.add(new Option(text, value));
}
};
// 刪除指定值的項
jQuery.fn.removeItem = function (value) {
if (this.isExistItem(value)) {
var count = this.size();
for (var i = 0; i < count; i++) {
if (jQuery(this).get(0).options[i].value == value) {
jQuery(this).get(0).remove(i);
break;
}
}
} else {
alert("待刪除的項不存在!");
}
};
// 刪除指定索引的項
jQuery.fn.removeIndex = function (index) {
var count = this.size();
if (index >= count || index < 0) {
alert("待刪除項索引超出範圍");
} else {
jQuery(this).get(0).remove(index);
}
};
// 刪除目前選中的項
jQuery.fn.removeSelected = function () {
this.removeIndex(this.getSelectedIndex());
};
// 清空全部
jQuery.fn.clearAll = function () {
jQuery(this).get(0).options.length = 0;
};
先引入 jQuery 主程式,再引入這支外掛,之後就能直接呼叫:
$("#sel").getSelectedText();
$("#sel").setSelectedValue("3");
$("#sel").addOption("新項目", "new_val");
$("#sel").removeItem("2");
$("#sel").clearAll();
這支外掛大量使用 jQuery(this).get(0) 取得原生 DOM 節點,再操作 options 集合。這麼做的原因是 select 的 options 集合有幾個方便的原生方法——options.add() 與 remove(index)——用 DOM API 比 jQuery 選擇器更直接。
另一個值得學的是它的防呆設計:每個方法在動手前都先檢查邊界,索引是否越界、值是否存在、清單是否為空。這讓外掛在資料不完整的情況下不會拋出錯誤,對不熟悉的呼叫者相當友善。
不過以現代標準來看,有兩處可以簡化。jQuery.fn.size() 這個名稱與 jQuery 內建的 .size() 方法相衝突,而後者已在 3.0 移除,建議改名為 optionCount() 避免混淆。另外,用 alert 提示錯誤並不是好習慣,正式專案應改為回傳 false 或拋出自訂錯誤,由呼叫端決定如何呈現。
如果不想自己維護,也可以直接用 jQuery 官方的 .val() 搭配少數幾個輔助函式,因為 select 的絕大多數操作,原生 jQuery 已經做得很好了:
$("#sel").val(); // 取值
$("#sel").val("3"); // 設值
$("#sel option").length; // 數量
$("#sel").empty().append("<option>..."); // 清空並重建
$("#sel option[value=3]").prop("selected", true); // 依值選中
真正需要自製外掛的,通常不是這些基礎操作,而是像「可搜尋下拉」或「連動的三層選單」這類複合行為。