object.options(index)或options.item(index)可以通過索引獲取options集合的指定項; select標(biāo)記還有一個屬性為selectedIndex,通過它可能獲取當(dāng)前選擇的option索引:object.selectedIndex
1.清空options集合
function optionsClear(object)
{ var length= object.options.length;
for(vari=length-1;i>=0;i--){
e.options.remove(i);
}
}
2.添加一項新option
function addOption(object)
{ object.add(new Option(label,value));
//使用options集合中最后一項獲取焦點
object.selectedIndex = object.lentht-1;
}
3.刪除options集合中指定的一項option
function removeOption(index)
{ if(index >= 0)
{ object.remove(index);
//使用options集合中最后一項獲取焦點
object.selectedIndex = object.lentht-1;
}
}
4.獲取當(dāng)前選定的option的真實值value
function getCurrentOptionValue(index)
{ if(index >= 0)
return object(index).value;
}
5.獲取當(dāng)前選定的option的顯示值label
functiongetCurrentOptionLabel(index)
{ if(index >= 0)
return object(index).text;
}
下面是日期的聯(lián)動下拉框:
function YYYYMMDD(){
//設(shè)置月份的集合
MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//清除年份下拉框的所有內(nèi)容,最主要包括空格
var yObj = document.form1.year
optionsClear(yObj);
//再給年下拉框賦內(nèi)容
yObj.options.add(new Option("請選擇",0));
for (var i = 1950; i<= new Date().getFullYear();i++) //以1950年為基準(zhǔn),到今年結(jié)束
document.form1.year.options.add(new Option(i ,i));
//清除月份下拉框的所有內(nèi)容,最主要包括空格
var mObj = document.form1.month;
optionsClear(mObj);