HTML 下拉框 select option 详解

一、基础理解:

   var e = document.getElementById("selectId");

   e. options= new Option("文本","值") ;

   //创建一个option对象,即在<select>标签中创建一个或多个<option value="值">文本</option>

   //options是个数组,里面可以存放多个<option value="值">文本</option>这样的标签

1:options[ ]数组的属性:

   length属性---------长度属性

   selectedIndex属性--------当前被选中的框中的文本的索引值,此索引值是内存自动分配的(0,1,2,3.....)对应(第一个文本值,第二个文本值,第三个文本值,第四个文本值..........)

2:单个option的属性(---obj.options[obj.selecedIndex]是指定的某个<option>标签,是一个---)

   text属性---------返回/指定 文本

   value属性------返回/指定 值,与<options value="...">一致。

   index属性-------返回下标,

   selected 属性-------返回/指定该对象是否被选中.通过指定 true 或者 false,可以动态的改变选中项

   defaultSelected 属性-----返回该对象默认是否被选中。true / false。

3:option的方法

   增加一个<option>标签-----obj.options.add(new("文本","值"));<增>

   删除一个<option>标签-----obj.options.remove(obj.selectedIndex)<删>

   获得一个<option>标签的文本-----obj.options[obj.selectedIndex].text<查>

   修改一个<option>标签的值-----obj.options[obj.selectedIndex]=new Option("新文本","新值")<改>

   删除所有<option>标签-----obj.options.length = 0

   获得一个<option>标签的值-----obj.options[obj.selectedIndex].value

注意:

   a:上面的写的是如这样类型的方法obj.options.function()而不写obj.funciton,是因为为了考虑在IE和FF 下的兼容,如obj.add()只能在IE中有效.

   b:obj.option中的option不需要大写,new Option中的Option需要大写


二 、应用

<html>
<head>
<script language="javascript">
function number(){
var obj = document.getElementById("mySelect");
//obj.options[obj.selectedIndex] = new Option("我的吃吃","4");//在当前选中的那个的值中改变
//obj.options.add(new Option("我的吃吃","4"));再添加一个option
//alert(obj.selectedIndex);//显示序号,option自己设置的
//obj.options[obj.selectedIndex].text = "我的吃吃";更改值
//obj.remove(obj.selectedIndex);删除功能
}
</script>
</head>
<body>
<select id="mySelect">
<option>我的包包</option>
<option>我的本本</option>
<option>我的油油</option>
<option>我的担子</option>
</select>
<input type="button" name="button" value="查看结果" οnclick="number();">
</body>
</html>

1.动态创建select

function createSelect(){

       var mySelect = document.createElement("select"); 
mySelect.id = "mySelect"; 
document.body.appendChild(mySelect);
}

2.添加选项option

function addOption(){

          //根据id查找对象,
var obj=document.getElementById('mySelect');

           //添加一个选项
obj.add(new      Option("文本","值"));    //这个只能在IE中有效
obj.options.add(new Option("text","value")); //这个兼容IE与firefox
}

3.删除所有选项option

function removeAll(){
var obj=document.getElementById('mySelect');

obj.options.length=0;

     }

4.删除一个选项option

function removeOne(){
var obj=document.getElementById('mySelect');

           //index,要删除选项的序号,这里取当前选中选项的序号

        var index=obj.selectedIndex;
obj.options.remove(index); 
}

5.获得选项option的值

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序号,取当前选中选项的序号

var val = obj.options[index].value;

6.获得选项option的文本

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序号,取当前选中选项的序号

var val = obj.options[index].text;

7.修改选项option

var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; //序号,取当前选中选项的序号

var val = obj.options[index]=new Option("新文本","新值");

8.删除select

function removeSelect(){
var mySelect = document.getElementById("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.getElementById(id)
}

function show()
{
var selectObj=$("area")
var myOption=document.createElement("option")
myOption.setAttribute("value","10")
myOption.appendChild(document.createTextNode("上海"))

var myOption1=document.createElement("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("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("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" on 
change="choice()">
</select>
</div>
<input type=button value="显示" οnclick="show()">
<input type=button value="计算结点" οnclick="calc()">
<input type=button value="删除" οnclick="remove()">
</body>
</html>














 

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值