javascript操作Select中的options集合

tonyscau發表於2008-05-06
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集合


Code highlighting produced by Actipro CodeHighlighter (freeware)


--&gtfunction optionsClear(object)
{
var length = object.options.length;
for(var i=length-1;i>=0;i--){
e.options.remove(i);
}
}

2.新增一項新option


Code highlighting produced by Actipro CodeHighlighter (freeware)


--&gtfunction addOption(object)
{
object.add(
new Option(label,value));
//使用options集合中最後一項獲取焦點
object.selectedIndex = object.lentht-1;
}

3.刪除options集合中指定的一項option


Code highlighting produced by Actipro CodeHighlighter (freeware)


--&gtfunction removeOption(index)
{
if(index >= 0)
{
object.remove(index);
//使用options集合中最後一項獲取焦點
object.selectedIndex = object.lentht-1;
}
}

4.獲取當前選定的option的真實值value


Code highlighting produced by Actipro CodeHighlighter (freeware)


--&gtfunction getCurrentOptionValue(index)
{
if(index >= 0)
return object(index).value;
}

5.獲取當前選定的option的顯示值label


Code highlighting produced by Actipro CodeHighlighter (freeware)


--&gtfunction getCurrentOptionLabel(index)
{
if(index >= 0)
return object(index).text;
}
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7558084/viewspace-1003476/,如需轉載,請註明出處,否則將追究法律責任。

相關文章