首页 新闻 会员 周边

cannt read property 'selectedindex' of null

0
[已解决问题] 解决于 2013-11-11 18:13

html:

<select id="selAnimal">
<option value="cat" selected>cat</option>
<option value="fish">fish</option>
<option value="bird">bird</option>
</select>

 

js:

var testValue = window.document.getElementById("selAnimal").selectedIndex;
(function(testValue){
alert(testValue);
})(testValue);

报错cannt read property 'selectedindex' of null 。 有解?

其实我就是想取这个select被选的值。。。

试了下var testValue = window.document.getElementById("selAnimal").value;也不对。。。

 

thx in advance.

Ruth/Christy的主页 Ruth/Christy | 菜鸟二级 | 园豆:222
提问于:2013-11-11 17:47
< >
分享
最佳答案
0

var selectIndex = document.getElementById("selAnimal").selectedIndex;//获得是第几个被选中了

var selectText = document.getElementById("selAnimal").options[selectIndex].text //获得被选中的项目的文本alert(selectText);//得到显示的值

奖励园豆:5
Zery | 大侠五级 |园豆:6151 | 2013-11-11 17:56

不顶用。

Ruth/Christy | 园豆:222 (菜鸟二级) | 2013-11-11 17:59

@Ruth/Christy:

把第一句加上 

Zery | 园豆:6151 (大侠五级) | 2013-11-11 18:04

@Zery-zhang: value其实也是能取到的,我重新写了个页面,就好了。可能之前的哪个页面上有冲突。谢谢了~

Ruth/Christy | 园豆:222 (菜鸟二级) | 2013-11-11 18:13
其他回答(1)
0
<html>
<head>
    <script>
        var showIt = function()
        {
            var sel=document.getElementById("selAnimal");
            var selvalue= sel.options[sel.options.selectedIndex].value;
            alert(selvalue);
        }
    </script>
</head>
<body>
    <select id="selAnimal">
        <option value="cat" selected>cat</option>
        <option value="fish">fish</option>
        <option value="bird">bird</option>
    </select>
    
    <button onclick="showIt();">click me</button>
</body>
</html>
会长 | 园豆:12401 (专家六级) | 2013-11-11 18:06

value其实也是能取到的,我重新写了个页面,就好了。可能之前的哪个页面上有冲突。谢谢了~

支持(0) 反对(0) Ruth/Christy | 园豆:222 (菜鸟二级) | 2013-11-11 18:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册