Javascript操作Select的方法

來源:互聯網
上載者:User

用法一:

<select name="week" onchange="location.href='login.jsp?temp='+this.value"> 
<option value="1">星期一</option> 
<option value="2">星期二</option> 
<option value="3">星期三</option> 
<option value="4">星期四</option> 
<option value="5">星期五</option> 
<option value="6">星期六</option> 
<option value="7">星期日</option> 
</select>

用法二:

JS動態改變select選擇變更option的index值
document.getElementById("louyuming").options[0].selected=true;

態刪除select中的所有options:
function deleteAllOptions(sel){
sel.options.length=0;
}
動態刪除select中的某一項option:
function deleteOption(sel,indx){
sel.options.remove(indx);
}
動態添加select中的項option:
function addOption(sel,text,value){
sel.options.add(new Option(text,value));
}
上面在IE和FireFox都能測試成功,希望以後可以用上。
===========================================

1判斷select選項中 是否存在Value="paraValue"的Item
2向select選項中 加入一個Item
3從select選項中 刪除一個Item
4刪除select中選中的項
5修改select選項中 value="paraValue"的text為"paraText"
6設定select中text="paraText"的第一個Item為選中
7設定select中value="paraValue"的Item為選中
8得到select的當前選中項的value
9得到select的當前選中項的text
10得到select的當前選中項的Index
11清空select的項
======================================================================
js 代碼

// 1.判斷select選項中 是否存在Value="paraValue"的Item        
function jsSelectIsExitItem(objSelect, objItemValue) {        
     var isExit = false;        
     for (var i = 0; i < objSelect.options.length; i++) {        
         if (objSelect.options[i].value == objItemValue) {        
             isExit = true;        
             break;        
         }        
     }        
     return isExit;        
}         
   
// 2.向select選項中 加入一個Item        
function jsAddItemToSelect(objSelect, objItemText, objItemValue) {        
     //判斷是否存在        
     if (jsSelectIsExitItem(objSelect, objItemValue)) {        
         alert("該Item的Value值已經存在");        
     } else {        
         var varItem = new Option(objItemText, objItemValue);      
         objSelect.options.add(varItem);     
         alert("成功加入");     
     }        
}        
   
// 3.從select選項中 刪除一個Item        
function jsRemoveItemFromSelect(objSelect, objItemValue) {        
     //判斷是否存在        
     if (jsSelectIsExitItem(objSelect, objItemValue)) {        
         for (var i = 0; i < objSelect.options.length; i++) {        
             if (objSelect.options[i].value == objItemValue) {        
                 objSelect.options.remove(i);        
                 break;        
             }        
         }        
         alert("成功刪除");        
     } else {        
         alert("該select中 不存在該項");        
     }        
}    
   
   
// 4.刪除select中選中的項    
function jsRemoveSelectedItemFromSelect(objSelect) {        
     var length = objSelect.options.length - 1;    
     for(var i = length; i >= 0; i--){    
         if(objSelect[i].selected == true){    
             objSelect.options[i] = null;    
         }    
     }    
}      
   
// 5.修改select選項中 value="paraValue"的text為"paraText"        
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {        
     //判斷是否存在        
     if (jsSelectIsExitItem(objSelect, objItemValue)) {        
         for (var i = 0; i < objSelect.options.length; i++) {        
             if (objSelect.options[i].value == objItemValue) {        
                 objSelect.options[i].text = objItemText;        
                 break;        
             }        
         }        
         alert("成功修改");        
     } else {        
         alert("該select中 不存在該項");        
     }        
}        
   
// 6.設定select中text="paraText"的第一個Item為選中        
function jsSelectItemByValue(objSelect, objItemText) {            
     //判斷是否存在        
     var isExit = false;        
     for (var i = 0; i < objSelect.options.length; i++) {        
         if (objSelect.options[i].text == objItemText) {        
             objSelect.options[i].selected = true;        
             isExit = true;        
             break;        
         }        
     }              
     //Show出結果        
     if (isExit) {        
         alert("成功選中");        
     } else {        
         alert("該select中 不存在該項");        
     }        
}        
   
// 7.設定select中value="paraValue"的Item為選中    
objSelect.value = objItemValue;    
       
// 8.得到select的當前選中項的value    
var currSelectValue = objSelect.value;    
       
// 9.得到select的當前選中項的text    
var currSelectText = objSelect.options[document.all.objSelect.selectedIndex].text;    
       
// 10.得到select的當前選中項的Index    
var currSelectIndex = objSelect.selectedIndex;    
       
// 11.清空select的項    
objSelect.options.length = 0; 

整個執行個體的完整代碼如下:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>javascript select options text value</title>
<meta name="keywords" content="javascript select options text value add modify delete set">
<meta name="description" content="javascript select options text value add modify delete set">
<script language="javascript">
<!--
// Author: i@lxl.cn
// Modify: i@cnlei.com
function watch_ini(){ // 初始
for(var i=0; i<arguments.length; i++){
   var oOption=new Option(arguments[i],arguments[i]);
   document.getElementById("MySelect")[i]=oOption;
}
}
function watch_add(f){ // 增加
   var oOption=new Option(f.word.value,f.word.value);
   f.keywords[f.keywords.length]=oOption;
}
function watch_sel(f){ // 編輯
f.word.value = f.keywords[f.keywords.selectedIndex].text;
}
function watch_mod(f){ // 修改
f.keywords[f.keywords.selectedIndex].text = f.word.value;
}
function watch_del(f){ // 刪除
f.keywords.remove(f.keywords.selectedIndex);
}
function watch_set(f){ // 儲存
var set = "";
for(var i=0; i<f.keywords.length; i++){
set += f.keywords[i].text + ";";
}
confirm(set);
}
//-->
</script>
</head>
<body>
<form name="watch" method="post" action="">
<select id="MySelect" name="keywords" size="10" onchange="watch_sel(this.form)"></select><br>
<script language="javascript">
<!--
watch_ini("我","你","妳","他","她","它","爾"); // 初始關鍵詞
//-->
</script>
<input type="text" name="word" /><br />
<input type="button" value="增加" onclick="watch_add(this.form);" />
<input type="button" value="修改" onclick="watch_mod(this.form);" />
<input type="button" value="刪除" onclick="watch_del(this.form);" />
<input type="button" value="儲存" onclick="watch_set(this.form);" />
</form>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.