表白:黑白圣堂血天使,天剑鬼刀阿修罗。 讲解对象:/js操作select 显示option的文本 作者:融水公子 rsgz ===
今天有一个需求就是 怎么获取这个下拉框列表的 选中的值呢?用js实现

这里面简单的思路就是
select_ele = document.getElementById('select')
index = select_ele.selectedIndex // 1
select_text = select_ele.options[index].text; // "萧炎"
获取其他option文本
select_ele.options[0].text;
select_ele.options[1].text;
select_ele.options[2].text;
获取option长度
select_ele.length //3
获取对应option源码
select_ele.item(2) // <option value="goldfish" selected="">Goldfish</option>
select_ele.namedItem('cat')); // <option value="cat" name="cat">Cat</option>
创建新option
var Macaw = document.createElement("option"); // 创建元素
Macaw.setAttribute("value", "macaw");
var newContent = document.createTextNode("Macaw"); // 创建文本
Macaw.appendChild(newContent); // 文本放进元素
selectEle.add(Macaw, 1);// Macaw index为1 元素放进select对象
selectEle.remove(2);// cat 被删除 === 公众号:不浪仙人 谢谢大家的支持!可以点击我的头像,进入我的空间浏览更多文章呢。建议大家360doc[www.360doc.com]注册一个账号登录,里面真的有很多优秀的文章,欢迎大家的到来。 ---
|