日期:2014-05-16  浏览次数:20373 次

Select控件中如何正确的获取选中的option呢?
我写了个测试方法,以下这个方法,不管选中的是哪项,alert的都是-1

function   getSelectedOption(obj){
        obj.selectedIndex   =   obj.selectedIndex;
        alert(obj.selectedIndex);
}

如果直接
function   getSelectedOption(obj){
        alert(obj.selectedIndex);
}

则选中第N项时,第一次alert时是-1,第二次之后alert的就是N,非常奇怪,
求解



------解决方案--------------------
在IE下测试,没有出现lz所说的情况,lz用的是什么浏览器?

<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function getSelectedOption(obj){
//obj.selectedIndex = obj.selectedIndex;
alert(obj.selectedIndex);
}
function add(){
var s=document.getElementById( "lbxAccepter ");
var n=hid.value;
hid.value=parseInt(n)+1;
var op=new Option(n,n);
s.add(op);
}
</script>
</head>
<body>
<select id= "lbxAccepter " onchange=getSelectedOption(this)>
</select>
<input type=button onclick=add()>
<input type=hidden id=hid value=0>
</body>
</html>