天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

javascript Select標記中options操作方法集合

Javascript操作Select標記中options集合
先來看看options集合的這幾個方法:
options.add(option)方法向集合里添加一項option對象;
options.remove(index)方法移除options集合中的指定項;
options(index)或options.item(index)可以通過索引獲取options集合的指定項;

Javascript代碼如下:

var selectTag = null; //select標記
var OPTONLENGTH = 10; //每次填充option數
var colls = []; //對select標記options的引用

window.onload = function(){
selectTag = document.getElementById("SelectBox"); //獲取select標記
colls = selectTag.options; //獲取引用
//initSelectBox(); //自初始化select.options
};

//使用隨機數填充select.options
function initSelectBox(){
var random = 0 ;
var optionItem = null;
var item = null;

if(colls.length > 0 && isClearOption()){
clearOptions(colls);
}

for(var i=0;i<OPTONLENGTH;i++){

random = Math.floor(Math.random()*9000)+1000;
item = new Option(random,random); //通過Option()構造函數創建option對象
selectTag.options.add(item); //添加到options集合中
}

watchState();
}
//添加新option項前是否清空當前options
function isClearOption(){
return document.getElementById("chkClear").checked;
}
//清空options集合
function clearOptions(colls){
var length = colls.length;
for(var i=length-1;i>=0;i--){
colls.remove(i);
}
}
//添加一項新option
function addOption(){
colls.add(createOption());
lastOptionOnFocus(colls.length-1);
watchState();
}
//創建一個option對象
function createOption(){
var random = Math.floor(Math.random()*9000)+1000;
return new Option(random,random);
}
//刪除options集合中指定的一項option
function removeOption(index){
if(index >= 0){
colls.remove(index);
lastOptionOnFocus(colls.length-1);
}
watchState();
}
//獲取當前選定的option索引
function getSelectedIndex(){
return selectTag.selectedIndex;
}
//獲取options集合的總數
function getOptionLength(){
return colls.length;
}
//獲取當前選定的option文本
function getCurrentOptionValue(index){
if(index >= 0)
return colls(index).value;
}
//獲取當前選定的option值
function getCurrentOptionText(index){
if(index >= 0)
return colls(index).text;
}
//使用options集合中最后一項獲取焦點
function lastOptionOnFocus(index){
selectTag.selectedIndex = index;
}
//顯示當select標記狀態
function watchState(){
var divWatch = document.getElementById("divWatch");
var innerHtml="";
innerHtml = "option總數:" + getOptionLength();
innerHtml += "<br/>當前項索引:" + getSelectedIndex();
innerHtml += "<br/>當前項文本:" + getCurrentOptionText(getSelectedIndex());
innerHtml += "<br/>當前項值:" + getCurrentOptionValue(getSelectedIndex());
divWatch.innerHTML = innerHtml;
divWatch.align = "justify";
}

注意到上面創建option項時,使用了Option()構造函數,這個構造函數有兩個版本的重載。
1、var option = new Option(text,value); //這里要大寫Option()
2、var option = new Option();
option.text = text;
option.value=value;
我個人比較喜歡第一種方法來創建option對象。
另外,select標記還有一個比較有用的屬性就是selectedIndex,通過它可能獲取當前選擇的option索引,或通過索引設置指定options集合中哪一項被選擇。
select.selctedIndex = select.options.length-1; //將options集合中最后一項選中
var selectedItem = select.options(select.selectedIndex);//獲取當前選中項
selectedItem.text; //選中項的文本
selectedItem.value; //選中項的值

<BODY>
<Select name="SelectBox">
</Select>
<hr/>
<div id="divWatch" style="background-color:beige;width=220;">
</div>
<hr/>
<h4>使用隨機數初始化SelectBox</h4>
<input type="button" value="Init" onclick="initSelectBox()"/> <input type="checkbox" name="chkClear"/>clear
<hr/>
<h4>添加option項</h4>
<input type="button" value="create" onclick="addOption()"/>
<hr/>
<h4>刪除option項</h4>
<input type="button" value="delete" onclick="removeOption(colls.length-1)"/>
</BODY>
檢測是否有選中
if(objSelect.selectedIndex > -1) {
//說明選中
} else {
//說明沒有選中
}

刪除被選中的項
objSelect.options[objSelect.selectedIndex] = null;

增加項
objSelect.options[objSelect.length] = new Option("你好","hello");

修改所選擇中的項
objSelect.options[objSelect.selectedIndex] = new Option("你好","hello");

得到所選擇項的文本
objSelect.options[objSelect.selectedIndex].text;

得到所選擇項的值
objSelect.options[objSelect.selectedIndex].value;

JavaScript技術javascript Select標記中options操作方法集合,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 囯产精品久久久久久久久蜜桃 | 精品人妻伦九区久久AAA片69 | 久久免费精品视频 | 伊人成综合网伊人222 | 果冻传媒2021一二三区 | 国产精品久AAAAA片 | 亚洲一区在线观看无码欧美 | 超碰97人人做人人爱网站 | 女攻男受高h全文肉肉 | 精品水蜜桃久久久久久久 | 搡女人免费免费视频观看 | 国产精品乱码一区二区三 | 99精品国产在热 | bbw videos 欧美老妇 | 精品欧美一区二区三区四区 | 神马伦理不卡午夜电影 | 夜色资源站国产www在线视频 | 国产成人在线小视频 | 变态露出野外调教 | 午夜国产精品影院在线观看 | AV无码九九久久 | 飘雪韩国在线观看免费高清完整版 | 神马老子影院午夜伦 | 久久人人爽人人片AV人成 | 亚洲一区日韩一区欧美一区a | 伊人久久大香线蕉资源 | 老版香蕉版下载 | 中文字幕无码乱人伦蜜桃 | 亚洲A片不卡无码久久尤物 亚洲a免费 | www.99在线| 国产精品成人啪精品视频免费观看 | 日本乱子伦一区二区三区 | 亚洲精品国产一区二区贰佰信息网 | 亚洲理论片在线中文字幕 | 伊人亚洲综合网色 | 国产日韩高清一区二区三区 | 秋霞影音先锋一区二区 | 免费国产综合视频在线看 | 欧美黄色一级 | 2019天天射干网站 | 亚洲 欧美 日本 国产 高清 |