object.options.add(new Option(label,value))方法向集合裡添加一項option對象;
object.options.remove(index)方法移除options集合中的指定項;
object.options(index)或options.item(index)可以通過索引擷取options集合的指定項;
select標記還有一個屬性為selectedIndex,通過它可能擷取當前選擇的option索引:object.selectedIndex
//1.清空options集合function optionsClear(object){ var length = object.options.length;for(var i=length-1;i>=0;i--){e.options.remove(i);}}//2.添加一項新optionfunction addOption(object){ object.add(new Option(label,value));//使用options集合中最後一項擷取焦點object.selectedIndex = object.lentht-1;}//3.刪除options集合中指定的一項optionfunction removeOption(index){ if(index >= 0){ object.remove(index);//使用options集合中最後一項擷取焦點object.selectedIndex = object.lentht-1;}}//4.擷取當前選定的option的真實值valuefunction getCurrentOptionValue(index){ if(index >= 0)return object(index).value;}//5.擷取當前選定的option的顯示值labelfunction getCurrentOptionLabel(index){ if(index >= 0)return object(index).text;}//下面是日期的聯動下拉框:function YYYYMMDD(){//設定月份的集合MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];//清除年份下拉框的所有內容,最主要包括空格var yObj = document.form1.yearoptionsClear(yObj);//再給年下拉框賦內容yObj.options.add(new Option("請選擇",0));for (var i = 1950; i <= new Date().getFullYear(); i++) //以1950年為基準,到今年結束document.form1.year.options.add(new Option(i , i));//清除月份下拉框的所有內容,最主要包括空格var mObj = document.form1.month;optionsClear(mObj);//再賦月份的下拉框document.form1.month.options.add(new Option("請選擇",0));for (var i = 1; i < 13; i++)document.form1.month.options.add(new Option(i,i));//賦年份的初始值document.form1.year.value = document.getElementsByName("YYYY")[0].value;//賦月份的初始值document.form1.month.value = document.getElementsByName("MM")[0].value;//賦日期下拉框var n = MonHead[document.form1.month.value-1];if (document.form1.month.value ==2 && IsPinYear(YYYYvalue)) n++;writeDay(n);//賦日期的初始值document.form1.date.value = document.getElementsByName("DD")[0].value;}//年發生變化時日期發生變化(主要是判斷閏平年)function YYYYDD(str) {var MMvalue = document.form1.month.options[document.form1.month.selectedIndex].value;if (MMvalue == "") {var e = document.form1.date;optionsClear(e);return;}var n = MonHead[MMvalue - 1];if (MMvalue == 2 && IsPinYear(str)) n++;writeDay(n)}//月發生變化時日期聯動function MMDD(str) {var YYYYvalue = document.form1.year.options[document.form1.year.selectedIndex].value;if (YYYYvalue == ""){var e = document.form1.date;optionsClear(e);return;}var n = MonHead[str - 1];if (str == 2 && IsPinYear(YYYYvalue)) n++;writeDay(n)}//據條件寫日期的下拉框function writeDay(n) {var e = document.form1.date;optionsClear(e);for (var i=1; i<(n+1); i++)e.options.add(new Option(i,i));}//判斷是否閏平年function IsPinYear(year) {return(0 == year%4 && (year0 !=0 || year@0 == 0));}//清除下拉框的所有內容function optionsClear(e) {var length = object.options.length;for(var i=length-1;i>=0;i--){e.options.remove(i);}}再送些福利
//1 檢測是否有選中if (objSelect.selectedIndex > - 1 ) {// 說明選中} else {// 說明沒有選中}//2 刪除被選中的項objSelect.options[objSelect.selectedIndex] = null ;//3 增加項objSelect.options[objSelect.length] = new Option( " 你好 " , " hello " );//4 修改所選擇中的項objSelect.options[objSelect.selectedIndex] = new Option( " 你好 " , " hello " );//5 得到所選擇項的文本objSelect.options[objSelect.selectedIndex].text;//6 得到所選擇項的值objSelect.options[objSelect.selectedIndex].value;
|