js 操做select和option常見用法

一、獲取選中select的value和text,html
javascript

<select id="mySelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>

經過如下script代碼s來獲取選中的value和text
html

$("#mySelect").val(); //獲取選中記錄的value值
$("#mySelect option:selected").text(); //獲取選中記錄的text值 

二、運用new Option("文本","值")方法添加選項option java

var obj = document.getElementById("mySelect");obj.add(new Option("4","4")); 

三、刪除全部選項option 數組

var obj = document.getElementById("mySelect");obj.options.length = 0; 

四、刪除選中選項option app

var obj = document.getElementById("mySelect");var index = obj.selectedIndex;obj.options.remove(index);

五、修改選中選項option post

var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options[index] = new Option("three",3); //更改對應的值
obj.options[index].selected = true; //保持選中狀態

六、刪除select ui

var obj = document.getElementById("mySelect");
obj.parentNode.removeChild(obj); //移除當前對象 

七、select選擇的響應事件 this

$("#mySelect").change(function(){ //添加所須要執行的操做代碼}) 

1.動態建立select spa

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

2.添加選項option firefox

function addOption(){
//根據id查找對象,
var obj=document.getElementByIdx_x('mySelect');
//添加一個選項
obj.add(new Option("文本","值")); //這個只能在IE中有效
obj.options.add(new Option("text","value")); //這個兼容IE與firefox
}

3.刪除全部選項option

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

4.刪除一個選項option

function removeOne(){
var obj=document.getElementByIdx_x('mySelect');
//index,要刪除選項的序號,這裏取當前選中選項的序號
var index=obj.selectedIndex;
obj.options.remove(index);
} 

5.得到選項option的值

var obj=document.getElementByIdx_x('mySelect');
var index=obj.selectedIndex; //序號,取當前選中選項的序號
var val = obj.options[index].value; 

6.得到選項option的文本

var obj=document.getElementByIdx_x('mySelect');
var index=obj.selectedIndex; //序號,取當前選中選項的序號
var val = obj.options[index].text;

 

7.修改選項option

var obj=document.getElementByIdx_x('mySelect');
var index=obj.selectedIndex; //序號,取當前選中選項的序號
var val = obj.options[index]=new Option("新文本","新值");

8.刪除select

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

整個實例的完整代碼以下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<head>
<script language=JavaScript>
function $(id)
{
return document.getElementByIdx_x(id)
}
function show()
{
var selectObj=$("area")
var myOption=document.createElement_x("option")
myOption.setAttribute("value","10")
myOption.appendChild(document.createTextNode("上海"))
var myOption1=document.createElement_x("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_x("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_x("select")
nj.add(new Option("玄武區","201"))
nj.add(new Option("白下區","202"))
nj.add(new Option("下關區","203"))
nj.add(new Option("棲霞區","204"))
$("context").appendChild(nj)
}
}
function calc()
{
var x=$("context").childNodes.length-1;
alert(x)
}
function remove()
{
var i=$("context").childNodes.length-1;
var remobj=$("context").childNodes[i];
remobj.removeNode(true)
}
</script>
<body>
<div id="context">
<select id="area" onchange="choice()">
</select>
</div>
<input type=button value="顯示" onclick="show()">
<input type=button value="計算結點" onclick="calc()">
<input type=button value="刪除" onclick="remove()">
</body>
</html> 

改進版:在select中添加、修改、刪除option元素

 1 function watch_ini(){ // 初始
 2 for(var i=0; i<arguments.length; i++){
 3 var word = document.createElement_x("OPTION");
 4 word.text = arguments[i];
 5 watch.keywords.add(word); // watch. is form name
 6 }
 7 }
 8 function watch_add(f){ // 增長
 9 var word = document.createElement_x("OPTION");
10 word.text = f.word.value;
11 f.keywords.add(word);
12 }

上述 add() 方法只在IE下有效,爲兼容FF和Opera,對上述代碼進行了一下改進,改動後代碼以下:

 1 function watch_ini(){ // 初始
 2 for(var i=0; i<arguments.length; i++){
 3 var oOption=new Option(arguments[i],arguments[i]);
 4 document.getElementByIdx_x("MySelect")[i]=oOption;
 5 }
 6 }
 7 function watch_add(f){ // 增長
 8 var oOption=new Option(f.word.value,f.word.value);
 9 f.keywords[f.keywords.length]=oOption;
10 }

整個實例的完整代碼以下

 1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
 2 <html>
 3 <head>
 4 <title>javascript select options text value</title>
 5 <meta name="keywords" content="javascript select options text value add modify delete set">
 6 <meta name="description" content="javascript select options text value add modify delete set">
 7 <script language="javascript">
 8 <!--
 9 function watch_ini(){ // 初始
10 for(var i=0; i<arguments.length; i++){
11 var oOption=new Option(arguments[i],arguments[i]);
12 document.getElementByIdx_x("MySelect")[i]=oOption;
13 }
14 }
15 function watch_add(f){ // 增長
16 var oOption=new Option(f.word.value,f.word.value);
17 f.keywords[f.keywords.length]=oOption;
18 }
19 function watch_sel(f){ // 編輯
20 f.word.value = f.keywords[f.keywords.selectedIndex].text;
21 }
22 function watch_mod(f){ // 修改
23 f.keywords[f.keywords.selectedIndex].text = f.word.value;
24 }
25 function watch_del(f){ // 刪除
26 f.keywords.remove(f.keywords.selectedIndex);
27 }
28 function watch_set(f){ // 保存
29 var set = "";
30 for(var i=0; i<f.keywords.length; i++){
31 set += f.keywords[i].text + ";";
32 }
33 confirm(set);
34 }
35 //-->
36 </script>
37 </head>
38 <body>
39 <form name="watch" method="post" action="">
40 <select id="MySelect" name="keywords" size="10" onchange="watch_sel(this.form)"></select><br>
41 <script language="javascript">
42 <!--
43 watch_ini("我","你","妳","他","她","它","爾"); // 初始關鍵詞
44 //-->
45 </script>
46 <input type="text" name="word" /><br />
47 <input type="button" value="增長" onclick="watch_add(this.form);" />
48 <input type="button" value="修改" onclick="watch_mod(this.form);" />
49 <input type="button" value="刪除" onclick="watch_del(this.form);" />
50 <input type="button" value="保存" onclick="watch_set(this.form);" />
51 </form> 

用一個字符串建立一個數組方法:

 1 <script language="javascript">
 2 </body>
 3 </html>
 4 <script>
 5 function spli(){
 6 datastr="2,2,3,5,6,6";
 7 var str= new Array();
 8 str=datastr.split(",");
 9 for (i=0;i<str.length ;i++ )
10 { document.write(str[i]+"<br/>"); }
11 }
12 spli();
13 </script>

 

PS:在ie上運行良好,在火狐上調試出現錯誤,緣由以下
obj.add(new Option("文本","值")); //這個只能在IE中有效
obj.options.add(new Option("text","value")); //這個兼容IE與firefox

此內容是本人在查找資料的時候找到的,寫得很詳細,給你們分享分享,並不是原創,請見諒!(感謝上述內容的提供者>_<)

相關文章
相關標籤/搜索