jQuery 的選擇器很強,但對 select 的 options 物件進行細部操作時,還是需要一組專用方法。以下把常見操作包成插件,做日期聯動這類需求時會省下不少重複程式碼。
/**
* 檔案名稱:jquery.liu.select.js
* 功能:jQuery 外掛,封裝 select 元素的常用操作
*/
// 取得選項數量
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;
}
};
// 判斷是否存在值為 value 的選項
jQuery.fn.isExistItem = function(value) {
var isExist = false;
var count = this.size();
for (var i = 0; i < count; i++) {
if (jQuery(this).get(0).options[i].value == value) {
isExist = true;
break;
}
}
return isExist;
};
// 新增一項(值重複時提示)
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;
};
$(document).ready(function(){
// 初始化年份
for (var y = 2024; y <= 2026; y++) {
$("#selYear").addOption(y + " 年", y);
}
// 年份變動時重建月份
$("#selYear").change(function(){
$("#selMonth").clearAll();
for (var m = 1; m <= 12; m++) {
$("#selMonth").addOption(m + " 月", m);
}
});
// 月份變動時依大小月決定天數
$("#selMonth").change(function(){
var y = parseInt($("#selYear").getSelectedValue());
var m = parseInt($("#selMonth").getSelectedValue());
var days = new Date(y, m, 0).getDate(); // 自動處理閏年二月
$("#selDay").clearAll();
for (var d = 1; d <= days; d++) {
$("#selDay").addOption(d + " 日", d);
}
});
});
1. 不要用 .size() 覆寫。這個插件定義了 jQuery.fn.size,但 jQuery 本身在 1.8 之前就有 .size() 方法(等同 .length),而它在 3.0 已被移除。在現代專案中請直接用 $("#sel").find("option").length,不要自己覆寫。
2. new Date(y, m, 0).getDate() 是求天數的經典技巧。JavaScript 的月份從 0 開始,所以「下一個月的第 0 天」就是「這個月的最後一天」,閏年判斷會自動處理,不必自己寫 if。
3. 大量選項改用文件片段。迴圈中逐次呼叫 options.add 會反覆觸發 DOM 更新。選項多時,先把整段 HTML 組好再一次 .html() 寫入,效能差很多。
4. 用 alert 提示不夠友善。插件中的錯誤處理都用了 alert,實務上建議改成回傳 false 或丟出自訂錯誤,讓呼叫端決定怎麼呈現訊息。