DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML和Xhtml
JavaScript 操作 select option 完整整理
編輯:HTML和Xhtml     

一、基礎理解

var e = document.getElementById("selectId");
e.options = new Option("文本","值");
//建立一個 option 物件,等同在 <select> 中加入 <option value="值">文本</option>
//options 是個集合,可存放多組 option

options 集合的屬性

length:長度。
selectedIndex:目前被選中項目的索引值,由瀏覽器自動分配(0,1,2,3……)對應第一、二、三、四個選項。

單一 option 的屬性(obj.options[obj.selectedIndex] 即目前選中的 option)

text:取得或設定顯示文字。
value:取得或設定值,與 <option value="..."> 一致。
index:傳回索引。
selected:取得或設定該項是否被選中,指定 true/false 可動態改變選中項。
defaultSelected:該項預設是否被選中。

option 的增刪改查

新增:obj.options.add(new Option("文本","值"));
刪除:obj.options.remove(obj.selectedIndex)
取值:obj.options[obj.selectedIndex].value
讀文字:obj.options[obj.selectedIndex].text
修改:obj.options[obj.selectedIndex] = new Option("新文本","新值")
清空全部:obj.options.length = 0

兩點注意:寫成 obj.options.function() 而不是 obj.function(),是為了兼顧 IE 與 Firefox——像 obj.add() 就只在 IE 有效;另外 obj.options 的 option 不需大寫,但 new Option 的 Option 必須大寫。

二、應用範例

<html>
<head>
<script language="javascript">
function number(){
var obj = document.getElementById("mySelect");
//obj.options[obj.selectedIndex] = new Option("我的吃吃","4");//改變目前選中項
//obj.options.add(new Option("我的吃吃","4"));//新增一個 option
//alert(obj.selectedIndex);//顯示序號
//obj.options[obj.selectedIndex].text = "我的吃吃";//更改文字
//obj.remove(obj.selectedIndex);//刪除
}
</script>
</head>
<body>
<select id="mySelect">
<option>我的包包</option>
<option>我的本本</option>
<option>我的油油</option>
<option>我的擔子</option>
</select>
<input type="button" name="button" value="查看結果" onclick="number();">
</body>
</html>

三、常用操作函式

動態建立 select:

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

新增選項:

function addOption(){
var obj=document.getElementById('mySelect');
obj.add(new Option("文本","值")); //僅 IE 有效
obj.options.add(new Option("text","value")); //相容 IE 與 Firefox
}

清空全部:

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

刪除一項:

function removeOne(){
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex;//取目前選項的序號
obj.options.remove(index);
}

取值與取文字:

var obj=document.getElementById('mySelect');
var index=obj.selectedIndex;
var val = obj.options[index].value;
var txt = obj.options[index].text;

修改一項:

var obj=document.getElementById('mySelect');
var index=obj.selectedIndex;
obj.options[index]=new Option("新文本","新值");

刪除整個 select:

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

四、進階:省轄聯動

<script language=JavaScript>
function $(id) { return document.getElementById(id) }
function show() {
var selectObj=$("area")
var myOption=document.createElement("option")
myOption.setAttribute("value","10")
myOption.appendChild(document.createTextNode("上海"))
var myOption1=document.createElement("option")
myOption1.setAttribute("value","100")
myOption1.appendChild(document.createTextNode("南京"))
selectObj.appendChild(myOption)
selectObj.appendChild(myOption1)
}
function choice() {
var index=$("area").selectedIndex;
var val=$("area").options[index].getAttribute("value")
if(val==10) {
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
var sh=document.createElement("select")
sh.add(new Option("浦東新區","101"))
sh.add(new Option("黃浦區","102"))
sh.add(new Option("徐匯區","103"))
sh.add(new Option("普陀區","104"))
$("context").appendChild(sh)
}
if(val==100) {
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
var nj=document.createElement("select")
nj.add(new Option("玄武區","201"))
nj.add(new Option("白下區","202"))
nj.add(new Option("下關區","203"))
nj.add(new Option("棲霞區","204"))
$("context").appendChild(nj)
}
}
</script>

五、以 jQuery + AJAX + JSON 實作聯動下拉

/** 建立聯動下拉列表 */
function linkAgeJson(selectId,method,temp,url){
$j.ajax({
type: "get",//使用 get 存取後端
dataType: "json",//回傳 JSON 格式
url: url,
data: "method=" + method+"&temp="+temp,
success: function(msg){
var data = msg.lists;
coverJsonToHtml(selectId,data);
}
});;
}

/** 將 JSON 資料轉成 HTML option */
function coverJsonToHtml(selectId,nodeArray){
var tempSelect=$j("#"+selectId);
isClearSelect(selectId,'0');
var tempOption=null;
for(var i=0;i<nodeArray.length;i++){
tempOption= $j('<option value="'+nodeArray[i].dm+'">'+nodeArray[i].mc+'</option> ');
tempSelect.append(tempOption);
}
getCpgjThgl(selectId,'thgjDm');
}

/** 清空下拉列表從指定索引之後的值 */
function isClearSelect(selectId,index){
var length=document.getElementById(selectId).options.length;
while(length!=index){
//長度會變動,所以必須重新取得
length=document.getElementById(selectId).options.length;
for(var i=index;i<length;i++)
document.getElementById(selectId).options.remove(i);
length=length/2;
}
}

/** 取得退化構件列表 */
function getCpgjThgl(selectId1,selectId2){
var obj1=document.getElementById(selectId1);
var obj2=document.getElementById(selectId2);
var len=obj1.options.length;
if(len==1){ return false; }
document.getElementById(selectId2).length=1;
for(var i=0;i<len; i++){
var option= obj1.options[i];
if(i!=obj1.selectedIndex){
obj2.appendChild(option.cloneNode(true));
}
}
}

對應的 HTML:

<TABLE width="100%" border=0 align="left" cellPadding=0 cellSpacing=1>
<tr>
<td class="Search_item_18"><span class="Edit_mustinput">*</span>引用軟件:</td>
<td class="Search_content_82">
<input name="yyrjMc" id="yyrjMc" type="text" class="Search_input" tabindex="3" size="30" >
<input name="yyrjDm" id="yyrjDm" type="hidden" >
<input type="button" class="Search_button_select"
onClick="linkAgeTree('linkage','yyrjtree','yyrjMc','yyrjDm','linkageTree','1');" value="選擇...">
</td>
</tr>
<tr>
<td class="Search_item"><span class="Edit_mustinput">*</span>引用分版:</td>
<td class="Search_content" id="yyfb">
<select name="yyfbDm" style="width:160" id="yyfbDm" onChange="getCpgjThgl('yyfbDm','thgjDm')">
</select>
</td>
</tr>
<tr>
<td class="Search_item">退化構件:</td>
<td class="Search_content" id="thgj">
<select name="thgjDm" style="width:160" id="thgjDm">
<option value="-1" selected>無</option>
</select>
</td>
</tr>
</TABLE>

最後這段 isClearSelect 用了「長度除以二」的折半策略來加速清空,聽起來聰明,但邏輯脆弱、不易維護。實務上直接用 selectId + '.length = index' 一行就能達到同樣目的,程式碼中 getCpgjThgl 裡註解掉的那行正是這個寫法。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved