javascript
js清除添加的下拉框html,Html下拉框的定义以及JS、Jquary取值、添加和移除
現(xiàn)在有一id=test的下拉框,怎么拿到選中的那個(gè)值呢?
分別使用javascript原生的方法和jquery方法
? //multiply設(shè)置可以多行顯示,size設(shè)置默認(rèn)顯示多少行。
text1
text2
code:
一:javascript原生的方法
1:拿到select對(duì)象: var? myselect=document.getElementById("test");
2:拿到選中項(xiàng)的索引:var index=myselect.selectedIndex?;?????????????// selectedIndex代表的是你所選中項(xiàng)的index
3:拿到選中項(xiàng)options的value:? myselect.options[index].value;
4:拿到選中項(xiàng)options的text:? myselect.options[index].text;
二:jquery方法(前提是已經(jīng)加載了jquery庫(kù))
1:var options=$("#test option:selected");? //獲取選中的項(xiàng)
2:alert(options.val());?? //拿到選中項(xiàng)的值
3:alert(options.text());?? //拿到選中項(xiàng)的文本
三:Js實(shí)現(xiàn)下拉框選項(xiàng)的新增和移除
1:拿到select對(duì)象: var? myselect=document.getElementById("test");
2:新增:myselect.options.Add(new options("text","value"));
3:移除:myselect.options.Remove(myselect.options[myselect.selectedIndex]);
4:清空:myselect.empty();
原文:http://www.cnblogs.com/xiaoerlang90/p/4314312.html
總結(jié)
以上是生活随笔為你收集整理的js清除添加的下拉框html,Html下拉框的定义以及JS、Jquary取值、添加和移除的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 睡前100个小故事文本
- 下一篇: 一千克等于多少斤,怎么算