欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

select標(biāo)簽設(shè)置默認(rèn)選中的選項(xiàng)方法

 更新時(shí)間:2018年03月02日 09:11:05   作者:function__  
下面小編就為大家分享一篇select標(biāo)簽設(shè)置默認(rèn)選中的選項(xiàng)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

方法有兩種。

第一種 通過<select>的屬性來設(shè)置選中項(xiàng),此方法可以在動(dòng)態(tài)語言如php在后臺(tái)根據(jù)需要控制輸出結(jié)果。

< select id = "sel" >
< option value = "1" >1</ option >
< option value = "2" selected = "selected" >2</ option >
< option value = "3" >3</ option >
</ select >

第二種 為通過前端js來控制選中的項(xiàng):

< script type = "text/javascript" >
function change(){
  document.getElementById("sel")[2].selected=true;
}
</ script >
< select id = "sel" >
< option value = "1" >1</ option >
< option value = "2" >2</ option >
< option value = "3" >3</ option >
</ select >
< input type = "button" value = "修改" onclick = "change()" />

獲取<select>標(biāo)簽選中項(xiàng)文本的js代碼為:

var val = document.all.Item.options[document.all.Item.selectedIndex].text
var i=document.getElementById( 'sel' ).options[document.getElementById( 'sel' ).selectedIndex].value;

一些其它操作<select>標(biāo)簽的技巧如下:

1)動(dòng)態(tài)創(chuàng)建select

function createSelect(){
var mySelect = document.createElement( "select" );
mySelect.id = "mySelect" ;
document.body.appendChild(mySelect);
}

2)添加選項(xiàng)option

function addOption(){
//根據(jù)id查找對(duì)象,
var obj=document.getElementById( 'mySelect' );
//添加一個(gè)選項(xiàng)
obj.add( new Option( "文本" , "值" ));
}

3)刪除所有選項(xiàng)option

function removeAll(){
var obj=document.getElementById( 'mySelect' );
obj.options.length=0;
}

4)刪除一個(gè)選項(xiàng)option

function removeOne(){
var obj=document.getElementById( 'mySelect' );
//index,要?jiǎng)h除選項(xiàng)的序號(hào),這里取當(dāng)前選中選項(xiàng)的序號(hào)
var index=obj.selectedIndex;
obj.options.remove(index);
}

5)獲得選項(xiàng)option的值

var obj=document.getElementById( 'mySelect' );
var index=obj.selectedIndex; //序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)
var val = obj.options[index].value;

6)獲得選項(xiàng)option的文本

var obj=document.getElementById( 'mySelect' );
var index=obj.selectedIndex; //序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)
var val = obj.options[index].text;

7)修改選項(xiàng)option

var obj=document.getElementById( 'mySelect' );
var index=obj.selectedIndex; //序號(hào),取當(dāng)前選中選項(xiàng)的序號(hào)
var val = obj.options[index]= new Option( "新文本" , "新值" );

8)刪除select

function removeSelect(){
var mySelect = document.getElementById( "mySelect" );
mySelect.parentNode.removeChild(mySelect);
}

以上這篇select標(biāo)簽設(shè)置默認(rèn)選中的選項(xiàng)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論