js 操作select和option常见用法

1、获取选中select的value和text,html

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

通过以下script代码s来获取选中的value和text

$("#mySelect").val(); //获取选中记录的value值
$("#mySelect option:selected").text(); //获取选中记录的text值 

2、运用new Option("文本","值")方法添加选项option

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

3、删除所有选项option

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

4、删除选中选项option

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

5、修改选中选项option

var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options[index] = new Option("three",3); //更改对应的值
obj.options[index].selected = true; //保持选中状态

6、删除select

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

7、select选择的响应事件

$("#mySelect").change(function(){ //添加所需要执行的操作代码}) 

1.动态创建select

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

2.添加选项option

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

此内容是本人在查找资料的时候找到的,写得很详细,给大家分享分享,并非原创,请见谅!(感谢上述内容的提供者>_<)

posted @ 2015-08-14 00:15  疯狂麦洛  阅读(17778)  评论(0编辑  收藏  举报