【问题标题】:Can't access the option value using javascript on Internet Explorer无法在 Internet Explorer 上使用 javascript 访问选项值
【发布时间】:2011-01-11 09:28:14
【问题描述】:

我有以下设置

"<form name=\"mylimit\" style=\"float:left\">
                <select name=\"limiter\" onChange=\"limit()\">
                <option selected=\"selected\">&nbsp;</option>"; ...

我编写了一个 js 脚本来访问 selectField 'limiter' 的选定值,如下所示:

 var w = document.mylimit.limiter.selectedIndex;

var url_add = document.mylimit.limiter.options[w].value;

//在每个浏览器上,除了 Internet Explorer 之外,我都会得到预期值。认为 IExplorer 不喜欢上面的粗体部分。有什么线索吗?

【问题讨论】:

  • 你试过 document.getElementById() 代替吗?

标签: javascript html internet-explorer select


【解决方案1】:

您尝试过其中一种

document.mylimit.limiter.options[document.mylimit.limiter.selectedIndex].value

document.getElementById('limiter').options[document.getElementById('limiter').selectedIndex].value

这应该对我有帮助

【讨论】:

    【解决方案2】:

    document.getElementById("limiter").value 也可以。

    【讨论】:

    • 答案不完整...下面的戈登做得很好。感谢您的帮助
    【解决方案3】:

    IE 正在寻找 value 属性。如果在选项标签上找不到 value="",则其他浏览器似乎默认显示为值的文本。以下是适用于所有主要浏览器的内容。

    <form name="mylimit" style="float:left">
        <select name="limiter" onChange="limit()">
            <option selected="selected" value='foo'>bar</option>
        </select>
    </form>
    
    <script>
        var index = document.mylimit.limiter.selectedIndex;
        var value = document.mylimit.limiter.options[index].value;
        alert(value); // Alerts 'foo'
    </script>
    

    这样您就可以为每个选项设置一个单独的值并显示。

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多