最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS中操作<select>標簽選的值圖文詳解

 更新時間:2023年10月27日 10:38:09   作者:軟件技術(shù)愛好者  
這篇文章主要給大家介紹了關于JS中操作<select>標簽選值的相關資料, 在HTML中,<Select>標簽下的內(nèi)容,代表下來列表選項,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

JS中操作<select>標簽選的值

<select>標簽是一種表單控件,用來創(chuàng)建下拉列表。在<select> 標簽內(nèi)可用 <option> 標簽定義下拉列表中的可用選項。下面給出一個基本下拉列表示例:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>基本下拉列表a</title>
</head>
<body>
    <select id="selectID" style="width:100px;height:30px">
        <option>項1</option>
        <option>項2</option>
        <option>項3</option>
        <option>項4</option>
    </select>       
</body>
</html>

保存文件名:簡單下拉列表示例a.html,用瀏覽器打開效果:

JS操作下拉列表中的選項

<select>標簽下的內(nèi)容,可以通過JS的操作,獲取其對象,獲取被選項的索引(index)、值(value)、內(nèi)容(text)

獲取select對象:

var myselect=document.getElementById("selectID");

其中,selectID標識<select>標簽id屬性值

2.獲取選中項的索引:

var index =myselect.selectedIndex; //selectedIndex代表的是你所選中項的 index

3.獲取選中項option的value:

myselect.options[index].value;

上句可去掉options[index].寫為myselect.value

4.獲取選中項option的text:

myselect.options[index].text;

5. 獲取選中項的其他值,如有:

<select id="select">
    <option value="A" url="http://www.baidu.com">第一個option</option>
    <option value="B" url="http://www.qq.com">第二個option</option>
</select>

想獲取的url:

myselect.options[index].getAttribute('url');

提示:上面是分步寫法,現(xiàn)在看看綜合寫法

對于上面3的綜合寫法是:

document.getElementById("selectID").value;

document.getElementById("selectID").options[document.getElementById("selectID").selectedIndex].value;

對于上面4的綜合寫法是:

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,用瀏覽器打開效果:

用JS將數(shù)組中的元素信息添加到下拉列表

先介紹將數(shù)組的元素信息添加到下拉列表用到的方法和屬性

select標簽對象的方法

add() 向下拉列表添加一個選項。

語法:selectobject.add(option,before)

remove() 從下拉列表中刪除一個選項.

語法: selectobject.remove(index)

Optiont標簽對象的屬性

defaultSelected 返回 selected屬性的默認值。

index 返回下拉列表中某個選項的索引位置。

Selected 設置或返回 selected 屬性的值。

text 設置或返回某個選項的純文本值。

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ù)項" onclick="addopt()">
    </form>
<script>
    var arr=new Array('項1','項2','項3','項4','項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ù)項添加到下拉列表.html,用瀏覽器打開效果:

總結(jié)

到此這篇關于JS中操作&lt;select&gt;標簽選的值的文章就介紹到這了,更多相關JS操作&lt;select&gt;標簽選值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

伊通| 顺昌县| 英超| 垦利县| 合江县| 泸溪县| 赤壁市| 莲花县| 博野县| 武清区| 清涧县| 桐城市| 买车| 自贡市| 天津市| 深泽县| 扎兰屯市| 邮箱| 天津市| 达尔| 汨罗市| 富蕴县| 清新县| 奉节县| 盘山县| 全州县| 横峰县| 鹤庆县| 林甸县| 鹰潭市| 双峰县| 中江县| 勐海县| 富蕴县| 广灵县| 池州市| 全南县| 邵阳市| 鹤峰县| 虞城县| 虞城县|