javascript操作select

jiestyle21發表於2011-10-19
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為選中 
document.all.objSelect.value = objItemValue; 

8.得到select的當前選中項的value 
var currSelectValue = document.all.objSelect.value; 

9.得到select的當前選中項的text 
var currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text; 

10.得到select的當前選中項的Index 
var currSelectIndex = document.all.objSelect.selectedIndex; 

11.清空select的項 
document.all.objSelect.options.length = 0; 


下面是測試程式碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>javascript select結合_指令碼之家_www.jb51.net</title> 
<script language="javascript" type="text/javascript"> 

function vtOption(){ 
var s1=document.getElementById("select1"); 
var sV=s1.options[s1.selectedIndex].value; 
var sT=s1.options[s1.selectedIndex].text; 
alert(sV+"\n"+sT); 
} 

function addOption1(){ 
var s1=document.getElementById("select1"); 
var newOp1=new Option; 
newOp1.value="3"; 
newOp1.text="這是3"; 
var newOp2=new Option; 
newOp2.value="4"; 
newOp2.text="這是4"; 
s1.add(newOp1); 
s1.add(newOp2); 
} 

function addOption2(){ 
var s1=document.getElementById("select1"); 
for(var i=3;i<5;i++){ 
var newOp1=new Option("這是"+i,i); 
//var newOp1=new Option; 
//newOp1.value=i; 
//newOp1.text="這是"+i; 
s1.add(newOp1); 
} 
} 

function addOption3(){ 
var s1=document.getElementById("select1"); 
s1.options.length=0;//清除以前的選項 
for(var i=3;i<7;i++){ 
var newOp1=new Option("這是"+i,i); 
//var newOp1=new Option; 
//newOp1.value=i; 
//newOp1.text="這是"+i; 
s1.add(newOp1); 
} 
} 

function remOption(){ 
var s1=document.getElementById("select1"); 
for(var i=s1.options.length;i>=0;i--){ 
s1.options.remove(i); 
} 
} 

function remOptionSelected(){ 
var s1=document.getElementById("select1"); 
var i=s1.selectedIndex; 
//alert(i); 
s1.options.remove(i); 
} 
</script> 
</head> 

<body> 
<select name="select" id="select1"> 
<option value="1">這是1</option> 
<option value="2">這是2</option> 
</select>
 
<a href="javascript:vtOption()">取值</a>  <a href="javascript:addOption1()">增加1</a>  <a href="javascript:addOption2()">增加2</a>  <a href="javascript:addOption3()">增加3</a>  <a href="javascript:remOption()">刪除選項</a> 
  <a href="javascript:remOptionSelected()">刪除選取選項</a> 
</body> 
</html> 




相關文章