温馨提示×

温馨提示×

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

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

javascript中options集合的用法

发布时间:2021-07-10 14:08:15 来源:亿速云 阅读:153 作者:chen 栏目:编程语言

本篇内容介绍了“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