javascript操作select
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>
相關文章
- JavaScript操作select控制元件JavaScript控制元件
- javascript操作Select中的options集合JavaScript
- JavaScript動態操作select下拉選單JavaScript
- javascript操作Select標記中options集合JavaScript
- 原生javascript操作select下拉選單程式碼例項JavaScript
- jquery 操作selectjQuery
- JavaScript select 事件JavaScript事件
- JavaScript select remove()JavaScriptREM
- JavaScript select typeJavaScript
- JavaScript select valueJavaScript
- JavaScript select multipleJavaScript
- Js操作Select大全JS
- JavaScript select add()JavaScript
- JavaScript select options 集合JavaScript
- JavaScript select新增optionJavaScript
- js 操作select和optionJS
- JavaScript select text 屬性JavaScript
- JavaScript select size 屬性JavaScript
- JavaScript select length 屬性JavaScript
- JQuery 對 Select option 的操作jQuery
- SQLServer DML操作阻塞SELECT查詢SQLServer
- jQuery操作select下拉常用程式碼jQuery
- jquery如何操作select下拉選單jQuery
- JS操作Radio與SelectJS
- 使用select監視update的操作
- Oracle DML(非select) 操作不commit 對select的影響OracleMIT
- jquery操作select(取值,設定選中)jQuery
- SQL學習筆記—非select操作SQL筆記
- jQuery操作select下拉選單程式碼jQuery
- Oracle 樹操作(select…start with…connect by…prior)Oracle
- JavaScript新增和刪除select下拉項JavaScript
- JavaScript 動態生成select下拉選單JavaScript
- JavaScript動態建立select下拉選單JavaScript
- JavaScript 騷操作JavaScript
- javascript事件操作JavaScript事件
- jQuery的radio,checkbox,select操作jQuery
- jQuery操作select下拉選單常用程式碼jQuery
- jQuery select下拉選單的相關操作jQuery