【问题标题】:Cross Browser Issue : document.getElementById().value not working in IE but works in Firefox跨浏览器问题:document.getElementById().value 在 IE 中不起作用,但在 Firefox 中起作用
【发布时间】:2012-11-19 03:34:53
【问题描述】:

我有 1 个下拉框[年份],2 个文本框 [startDate, endDate],每当用户从下拉菜单中选择年份时,文本框 startDate 应自动填充到 01/01/+yearendDate12/31/+year。下面提到的脚本在 Firefox 中运行良好,但在 IE 中 getElementById('ff5').value 不起作用,有什么建议吗?

我的脚本:

<script>
function autoPopulateDate(value, startDt,endDt){
      document.getElementById(startDt).value='01/01/'+value;
      document.getElementById(endDt).value='12/31/'+value;
}
</script>

HTML 代码:

<tr>
    <td>
        <select onchange="autoPopulateDate(this.value,'ff5','ff6')" size="1" name="ff4" id="ff4"><option value="">--&gt;select value&lt;--</option><option value="2005">2005</option>
        <option value="2006">2006</option>
        <option value="2007">2007</option>
        <option value="2008">2008</option>

        <option value="ALL">ALL</option>
        </select>
    </td>
</tr>
<tr>
    <td ><font class="rtabletext">Savings Start Date: </font></td>
    <td >
        <input type="text" value="" name="ff5" id="ff5" maxlength="50" size="10" class="text">
    </td>
</tr>
<tr>
    <td><font class="rtabletext">Savings End Date: </font></td>
    <td>
        <input type="text" value="" name="ff6" id="ff6" maxlength="50" size="10" class="text">
    </td>
</tr>

【问题讨论】:

  • 是的,但是 IE 是我需要支持的浏览器。
  • @Rachel 在 IE 中为我工作 (8)。
  • 我用的是IE7,没有其他的JavaScript页面,实际上报表工具需要这个功能。
  • 在 IE 7 中运行良好。您的文档中是否有其他元素的 id 或名称为“ff5”?
  • 不,我没有其他元素的 id 或名称为 ff5

标签: javascript html cross-browser


【解决方案1】:

IE [至少某些版本和呈现模式] 希望您通过表单集合访问表单成员。

document.forms['someform'].elements['someelement'].value

或者,您可以使用一些 ajax 库(例如 http://www.asp.net/ajax )并使用该库元素检索方法,因为这些通常会考虑浏览器兼容性...

$get('element')

【讨论】:

  • @KristoferA 不,没必要,代码工作得很好。请参阅@mu 的小提琴,在他对该问题的评论中链接。
  • +1,奇怪的是,如果我尝试使用 document.forms['someform'].elements['someelement'].value 访问,它现在对我有用,谢谢 KristoferA
  • @Rachel 没什么奇怪的,只是浏览器兼容性问题。更高版本的 IE 有可能(甚至很可能)已与其他浏览器保持一致。换句话说,您的原始代码可能在 IE8/IE9(在标准模式下)中运行,但在 IE7/6/5/4/3 甚至新浏览器中(如果页面以怪癖或兼容模式呈现)则无法运行。
  • @Pointy 你能澄清一下你认为我的答案的哪些部分不正确,为什么?
  • @KristoferA "document.getElementById()" 方法在 Internet Explorer 中运行良好,至少可以追溯到 IE 6,几乎可以肯定也可以追溯到 IE 5。
猜你喜欢
  • 2011-07-22
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多