ITPub博客

首页 > 应用开发 > Java > javascript操作Select中的options集合

javascript操作Select中的options集合

原创 Java 作者:tonyscau 时间:2008-05-06 14:36:57 0 删除 编辑
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)
http://www.CodeHighlighter.com/

-->function 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)
http://www.CodeHighlighter.com/

-->function addOption(object)
{
object.add(
new Option(label,value));
//使用options集合中最后一项获取焦点
object.selectedIndex = object.lentht-1;
}

3.删除options集合中指定的一项option


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->function removeOption(index)
{
if(index >= 0)
{
object.remove(index);
//使用options集合中最后一项获取焦点
object.selectedIndex = object.lentht-1;
}
}

4.获取当前选定的option的真实值value


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

5.获取当前选定的option的显示值label


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7558084/viewspace-1003476/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-24

  • 博文量
    21
  • 访问量
    64995