HTML中Select的使用

mouqj發表於2007-09-01





<!--手工新增一個Select--&gt
<!--1 uses the SELECT element to create a drop-down list box--&gt


<!--2 select 元素建立了多項選擇列表框,方法是設定了 SIZE 和 MULTIPLE 標籤屬性。要獲得多項選擇列表框的選中選項,則須遍歷 options 集合並檢查 SELECTED 是否被設為 true。--&gt


<!--3 以下演示Option的用法--&gt



附:一些Select的技巧

1.動態建立select

function createSelect(){

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

2.新增選項option

function addOption(){

//根據id查詢物件,
var obj=document.getElementById('mySelect');

//新增一個選項
obj.add(new Option("文字","值"));
}

3.刪除所有選項option

function removeAll(){
var obj=document.getElementById('mySelect');

obj.options.length=0;

}

4.刪除一個選項option

function removeOne(){
var obj=document.getElementById('mySelect');

//index,要刪除選項的序號,這裡取當前選中選項的序號

var index=obj.selectedIndex;
obj.options.remove(index);

}

5.獲得選項option的值

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序號,取當前選中選項的序號

var val = obj.options[index].value;

6.獲得選項option的文字

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序號,取當前選中選項的序號

var val = obj.options[index].text;

7.修改選項option

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序號,取當前選中選項的序號

var val = obj.options[index]=new Option("新文字","新值");

8.刪除select

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

1HTML中Select的使用1 檢測是否有選中 2HTML中Select的使用HTML中Select的使用if(objSelect.selectedIndex > -1) HTML中Select的使用{ 3HTML中Select的使用//說明選中 4HTML中Select的使用HTML中Select的使用} else HTML中Select的使用{ 5HTML中Select的使用//說明沒有選中 6HTML中Select的使用} 7HTML中Select的使用 8HTML中Select的使用2 刪除被選中的項 9HTML中Select的使用objSelect.options[objSelect.selectedIndex] = null;10HTML中Select的使用11HTML中Select的使用3 增加項12HTML中Select的使用objSelect.options[objSelect.length] = new Option("你好","hello");13HTML中Select的使用14HTML中Select的使用4 修改所選擇中的項15HTML中Select的使用objSelect.options[objSelect.selectedIndex] = new Option("你好","hello");16HTML中Select的使用17HTML中Select的使用5 得到所選擇項的文字18HTML中Select的使用objSelect.options[objSelect.selectedIndex].text;19HTML中Select的使用20HTML中Select的使用6 得到所選擇項的值21HTML中Select的使用objSelect.options[objSelect.selectedIndex].value;22HTML中Select的使用

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11049438/viewspace-967496/,如需轉載,請註明出處,否則將追究法律責任。

相關文章