温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javascript中的options集合怎么用

发布时间:2022-04-26 10:40:11 来源:亿速云 阅读:166 作者:zzz 栏目:大数据

今天小编给大家分享一下javascript中的options集合怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

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/

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

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

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

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

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


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

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

以上就是“javascript中的options集合怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI