【问题标题】:Link for Dropdown下拉链接
【发布时间】:2013-07-23 12:52:47
【问题描述】:

我想选择带有链接的组合的索引。
我试过这个:

<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
我能怎么做?代码是一样的。但是负载不工作,按钮工作。嗯?

【问题讨论】:

  • 为什么要在“onclick”处理程序中评估代码?
  • 因为,我想确定代码是否正常工作。 :)
  • 我不确定你如何通过评估代码来实现这一点:)
  • 我找到了,谢谢你的帮助:)

标签: javascript select load selectedindex


【解决方案1】:

没有 selectedIndex 属性。您可以通过设置

selected

选项元素的属性

【讨论】:

【解决方案2】:

哦,我找到了!
现在,我的代码是这样的:

<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>

感谢所有愿意帮助我的人! :)

【讨论】:

    猜你喜欢
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多