JS中操作<select>標(biāo)簽選的值圖文詳解
JS中操作<select>標(biāo)簽選的值
<select>標(biāo)簽是一種表單控件,用來(lái)創(chuàng)建下拉列表。在<select> 標(biāo)簽內(nèi)可用 <option> 標(biāo)簽定義下拉列表中的可用選項(xiàng)。下面給出一個(gè)基本下拉列表示例:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>基本下拉列表a</title> </head> <body> <select id="selectID" style="width:100px;height:30px"> <option>項(xiàng)1</option> <option>項(xiàng)2</option> <option>項(xiàng)3</option> <option>項(xiàng)4</option> </select> </body> </html>
保存文件名:簡(jiǎn)單下拉列表示例a.html,用瀏覽器打開(kāi)效果:
JS操作下拉列表中的選項(xiàng)
<select>標(biāo)簽下的內(nèi)容,可以通過(guò)JS的操作,獲取其對(duì)象,獲取被選項(xiàng)的索引(index)、值(value)、內(nèi)容(text)
獲取select對(duì)象:
var myselect=document.getElementById("selectID");
其中,selectID標(biāo)識(shí)<select>標(biāo)簽id屬性值
2.獲取選中項(xiàng)的索引:
var index =myselect.selectedIndex; //selectedIndex代表的是你所選中項(xiàng)的 index
3.獲取選中項(xiàng)option的value:
myselect.options[index].value;
上句可去掉options[index].寫(xiě)為myselect.value
4.獲取選中項(xiàng)option的text:
myselect.options[index].text;
5. 獲取選中項(xiàng)的其他值,如有:
<select id="select"> <option value="A" url="http://www.baidu.com">第一個(gè)option</option> <option value="B" url="http://www.qq.com">第二個(gè)option</option> </select>
想獲取的url:
myselect.options[index].getAttribute('url');
提示:上面是分步寫(xiě)法,現(xiàn)在看看綜合寫(xiě)法
對(duì)于上面3的綜合寫(xiě)法是:
document.getElementById("selectID").value;
或
document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value;
對(duì)于上面4的綜合寫(xiě)法是:
document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].text
下面給出從下拉列表中選擇圖片顯示的示例源碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>演示</title> <style> div{ margin:20px; text-align:center; } </style> <script> function show() { document.getElementById("imgID").src = document.getElementById("selectID").value; } </script> </head> <body> <div > 雪景 <select id="selectID" onchange="show()" style="width:100px;height:30px"> <option value="./雪1.png">雪1</option> <option value="./雪2.png">雪2</option> <option value="./雪3.png">雪3</option> </select> <br> <img id="imgID" src="雪1.png" /> </div> </body> </html>
保存文件名:從下拉列表中選擇圖片顯示1b.html,用瀏覽器打開(kāi)效果:
用JS將數(shù)組中的元素信息添加到下拉列表
先介紹將數(shù)組的元素信息添加到下拉列表用到的方法和屬性
select標(biāo)簽對(duì)象的方法
add() 向下拉列表添加一個(gè)選項(xiàng)。
語(yǔ)法:selectobject.add(option,before)
remove() 從下拉列表中刪除一個(gè)選項(xiàng).
語(yǔ)法: selectobject.remove(index)
Optiont標(biāo)簽對(duì)象的屬性
defaultSelected 返回 selected屬性的默認(rèn)值。
index 返回下拉列表中某個(gè)選項(xiàng)的索引位置。
Selected 設(shè)置或返回 selected 屬性的值。
text 設(shè)置或返回某個(gè)選項(xiàng)的純文本值。
JS將數(shù)組的的元素信息添加到下拉列表,示例源碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>示例</title> </head> <body> <form name="form1" action=""> <select name="sel" style="width:100px;height:30px"> </select><br> <input type="button" value="加載數(shù)組的數(shù)據(jù)項(xiàng)" onclick="addopt()"> </form> <script> var arr=new Array('項(xiàng)1','項(xiàng)2','項(xiàng)3','項(xiàng)4','項(xiàng)5') var counts=arr.length; function addopt(){ for(var i=0;i<counts;i++){ // document.form1.sel.options[i]=new Option (arr[i],i) var opt=document.createElement('option') opt.text=arr[i] opt.value=i; document.form1.sel.add(opt,undefined) } } </script> </body> </html>
保存文件名:數(shù)組的數(shù)據(jù)項(xiàng)添加到下拉列表.html,用瀏覽器打開(kāi)效果:
總結(jié)
到此這篇關(guān)于JS中操作<select>標(biāo)簽選的值的文章就介紹到這了,更多相關(guān)JS操作<select>標(biāo)簽選值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序用戶授權(quán)彈窗 拒絕時(shí)引導(dǎo)用戶重新授權(quán)實(shí)現(xiàn)
我們?cè)陂_(kāi)發(fā)小程序時(shí),如果想獲取用戶信息,就需要獲取用的授權(quán),如果用戶誤點(diǎn)了拒絕授權(quán),我們?cè)趺礃尤フ_的引導(dǎo)用戶重新授權(quán)呢。今天就來(lái)給大家講講如果正確的引導(dǎo)用戶授權(quán),需要的朋友可以參考下2019-07-07Nuxt.js實(shí)現(xiàn)校驗(yàn)訪問(wèn)瀏覽器類型的中間件
Nuxt.js 就是一個(gè)Vue的服務(wù)端渲染框架,和React的服務(wù)端渲染框架類似。這篇文章主要介紹了Nuxt.js實(shí)現(xiàn)校驗(yàn)訪問(wèn)瀏覽器類型的中間件,需要的朋友可以參考下2018-08-08js實(shí)現(xiàn)模擬銀行卡賬號(hào)輸入顯示效果
這篇文章主要介紹了js實(shí)現(xiàn)模擬銀行卡賬號(hào)輸入顯示效果,涉及JavaScript正則匹配與字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11通過(guò)函數(shù)作用域和塊級(jí)作用域看javascript的作用域鏈
這篇文章給大家分享了通過(guò)函數(shù)作用域和塊級(jí)作用域看javascript的作用域鏈的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友參考學(xué)習(xí)下。2018-08-08layer.alert回調(diào)函數(shù)執(zhí)行關(guān)閉彈窗的實(shí)例
今天小編就為大家分享一篇layer.alert回調(diào)函數(shù)執(zhí)行關(guān)閉彈窗的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09