多选Select排序
?
根據下拉框的選擇排序
///初始化排序框
///GroupMakeUp_UserID,待排序的框的實例
///sel,排序選擇框
///
function InitSortCombox(GroupMakeUp_UserID, sel) {
??? if (sel != "undefine" && sel != null) {
??????? var GroupMakeUp_UserIDSort = document.getElementById(sel);
??????? //首先清空原有的值
??????? GroupMakeUp_UserIDSort.options.length = 0;
??????? CreateOption(GroupMakeUp_UserIDSort, "請選擇", "0");
??????? for (j = 0; j < GroupMakeUp_UserID.options.length; j++) {
??????????? //向GroupMakeUp_UserIDSort中添加項(排序框)
??????????? var s = GroupMakeUp_UserID.options[j].value.split("|");
??????????? if (Exists(GroupMakeUp_UserIDSort, s[0])) {
??????????????? CreateOption(GroupMakeUp_UserIDSort, s[1], s[0]);
??????????? }
??????? }
??? }
}
?
///排序
function Sort_GroupMakeUp_UserID(param, src) {
??? var oSel = document.getElementById(src);
?
??? // alert(param.value);
??? var arr = new Array(); // 這是關鍵部分
??? // 將select中的所有option的value值將保存在Array中
??? for (var i = 0; i < oSel.options.length; i++) {
??????? // 如果需要對option中的文本排序,可以改為arr[i] = oSel.options[i].text;
??????? arr[i] = { key: oSel.options[i].innerText, value: oSel.options[i].value };
??? }
??? //js Array排序
??? arr.sort(function(a, b) { return a.value.indexOf(param.value) == -1 ? 1 : -1; }); // 開始排序
??? // 清空Select中全部Option
??? oSel.options.length = 0;
?
??? // 將排序后的數組重新添加到Select中
??? for (i = 0; i < arr.length; i++) {
??????? CreateOption(oSel, arr[i].key, arr[i].value);
??? }
}
?
//是否已經存在
function Exists(src, param) {
??? for (e = 0; e < src.options.length; e++) {
??????? if (src.options[e].value == param) {
??????????? return false;
??????? }
??? }
??? return true;
}
//創建新的OPTION對象
function CreateOption(src, text, value) {
??? var oOption = document.createElement("OPTION");
??? src.options.add(oOption);
??? oOption.innerText = text;
??? oOption.value = value;
}
?
轉載于:https://www.cnblogs.com/jimtomjim/archive/2009/08/21/1551542.html
總結
以上是生活随笔為你收集整理的多选Select排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 由DWR1.0到DWR2.0常出现的问题
- 下一篇: 最近面试,笔试题中的一道sql题