一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

javascript Select標(biāo)記中options操作方法集合

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

Javascript代碼如下:

var selectTag = null; //select標(biāo)記
var OPTONLENGTH = 10; //每次填充option數(shù)
var colls = []; //對select標(biāo)記options的引用

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

//使用隨機數(shù)填充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()構(gòu)造函數(shù)創(chuàng)建option對象
selectTag.options.add(item); //添加到options集合中
}

watchState();
}
//添加新option項前是否清空當(dāng)前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();
}
//創(chuàng)建一個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();
}
//獲取當(dāng)前選定的option索引
function getSelectedIndex(){
return selectTag.selectedIndex;
}
//獲取options集合的總數(shù)
function getOptionLength(){
return colls.length;
}
//獲取當(dāng)前選定的option文本
function getCurrentOptionValue(index){
if(index >= 0)
return colls(index).value;
}
//獲取當(dāng)前選定的option值
function getCurrentOptionText(index){
if(index >= 0)
return colls(index).text;
}
//使用options集合中最后一項獲取焦點
function lastOptionOnFocus(index){
selectTag.selectedIndex = index;
}
//顯示當(dāng)select標(biāo)記狀態(tài)
function watchState(){
var divWatch = document.getElementById("divWatch");
var innerHtml="";
innerHtml = "option總數(shù):" + getOptionLength();
innerHtml += "<br/>當(dāng)前項索引:" + getSelectedIndex();
innerHtml += "<br/>當(dāng)前項文本:" + getCurrentOptionText(getSelectedIndex());
innerHtml += "<br/>當(dāng)前項值:" + getCurrentOptionValue(getSelectedIndex());
divWatch.innerHTML = innerHtml;
divWatch.align = "justify";
}

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

<BODY>
<Select name="SelectBox">
</Select>
<hr/>
<div id="divWatch" style="background-color:beige;width=220;">
</div>
<hr/>
<h4>使用隨機數(shù)初始化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技術(shù)javascript Select標(biāo)記中options操作方法集合,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 小视频免费在线观看 | 9l国产精品久久久久麻豆 | 九九精品视频在线观看 | 亚洲第一免费网站 | 欧美激情国产一区在线不卡 | 色哟哟网 | 国产精品免费观看视频 | 国产精品手机在线亚洲 | 亚洲黄色在线观看视频 | 国产小视频网址 | 四虎永久在线精品免费影视 | 人人澡人人擦人人免费 | 亚洲国产高清一区二区三区 | 久久综合成人 | 亚洲二区在线播放 | 五月婷婷网址 | 黄色免费观看网站 | 丁香婷婷综合五月综合色啪 | 国产自在线 | 中文乱码精品一区二区三区 | 精品成人资源在线观看 | 亚亚洲乱码一二三四区 | 91精品视频在线观看免费 | 国产精品jizz视频 | 日本一区二区三区久久久久 | 国产精品亚洲欧美一级久久精品 | 99久久精品国产亚洲 | 四虎国产精品永久地址99 | 91精品福利老司机在线观看 | 亚洲精品在线播放视频 | 亚洲综合色婷婷 | 色啊色| 国产亚洲精品国产第一 | 又黄又爽又猛大片录像 | 色呦呦网站在线观看 | 国产成人亚洲综合小说区 | 亚洲不卡一区二区三区 | 成人午夜精品视频在线观看 | 国产中文字幕一区 | 国产精品乱码一区二区三区 | 国产精品路线1路线2路线 |