我想选择带链接的组合索引
我试过这个:
<script>
eval(document.location.hash.substring(1));
function poke(num, zat) {
var selObj = document.getElementById('okey');
selObj.selectedIndex = num - 1;
var selObj = document.getElementById('nope');
selObj.selectedIndex = zat - 1;
}
</script>
<select id="okey">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
<select id="nope">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
<button onclick="eval(document.location.hash.substring(1));">okey</button>
按钮工作正常,但当我尝试加载页面时,它显示错误。
错误是这样的:
Uncaught TypeError: Cannot set property 'selectedIndex' of null
我能怎么做?代码是一样的。但是负载不起作用,按钮正在工作。咦?
答案 0 :(得分:0)
没有selectedIndex属性。您可以通过设置
来设置所选选项selected
选项元素的属性
答案 1 :(得分:0)
哦,我找到了!
现在,我的代码是这样的:
<body onload="eval(document.location.hash.substring(1));">
<script>
function poke(num, zat) {
var selObj = document.getElementById('okey');
selObj.selectedIndex = num - 1;
var selObj = document.getElementById('nope');
selObj.selectedIndex = zat - 1;
}
</script>
<select id="okey">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
<select id="nope">
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
感谢所有想要帮助我的人! :)