Html SCRIPT5007:无法获取未定义或空引用的属性“值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18124574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:58:04  来源:igfitidea点击:

SCRIPT5007: Unable to get property 'value' of undefined or null reference

javascripthtmlinternet-explorer-10

提问by user2706

I have a html form that appears not to be functional in IE10. when click on go button debugging returns:"SCRIPT5007: Unable to get property 'value' of undefined or null reference" I have added this tag in my html page

我有一个 html 表单,它在 IE10 中似乎不起作用。当点击 go 按钮调试返回:“SCRIPT5007:无法获得未定义或空引用的属性‘值’”我在我的 html 页面中添加了这个标签

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />.

html contains dropdown menu option for frequency in myform.After selecting any option in dropdown i am calling below javascript function.

html 包含 myform 中频率的下拉菜单选项。在下拉列表中选择任何选项后,我在 javascript 函数下方调用。

function chooseFreqMenu(freqValues,freqTexts)
{
    var cf = document.myform;
    var freq1=document.myform.freq.value; // freq1="d"
    alert(freq1);// displaying freq1 value is "d" (ex:selected 'd' in html dropdown)
// checking some condition for freqValues.
    for(var i=4;i<freqValues.length;i++)
            selectStr += '<option value="'+freqValues[i]+'">'+freqTexts[i]+'\n';
    }
    selectStr += '</select>';
    // Assinging the selectedStr to innerHTML. After this statement i am getting empty for freq1 value. 
    //Its working fine in IE<10 browsers
    document.all.freqSel.innerHTML = selectStr; 
    alert(freq1); // displaying freq1 value is empty.
}   

Before sending myform to chooseFreqmenu, myform contains freq value="d"(assume i selected 'd' in dropdown) After the above function myform doesn't contain freq value. After the above function passing myform to buildQueryStr.

在将 myform 发送到chooseFreqmenu 之前,myform 包含 freq value="d"(假设我在下拉列表中选择了 'd') 在上述函数之后 myform 不包含 freq 值。在上面的函数将 myform 传递给 buildQueryStr 之后。

function buildQueryStr(myform)
{

var freq=myform.freq.value; //Getting an error SCRIPT5007: Unable to get property 'value' of undefined or null reference in this line

//some other fields.

}

How to fix this issue?

如何解决这个问题?

Any suggestions?? Thanks in Advance.

有什么建议??提前致谢。

回答by bastos.sergio

I don't think IE 10 supports acessing elements with the myform.freq.valuesyntax anymore.

我认为 IE 10不再支持使用myform.freq.value语法访问元素。

The standard way of accessing Elements (this is supported in all browsers including IE) is with the document.getElementByIdfunction

访问元素的标准方式(包括 IE 在内的所有浏览器都支持)是使用document.getElementById函数

function buildQueryStr(myform) {

    //var freq=myform.freq.value; //Getting an error SCRIPT5007: Unable to get property 'value' of undefined or null reference in this line
    var freq=document.getElementById("freq").value;

    //some other fields.
}

回答by Gufino2

First: you shouldn't be using document.all, not in 2013 :) It's an old Microsoft extension and I think it's deprecated. Use getElementById.

首先:您不应该使用document.all,而不是在 2013 年:) 这是一个旧的 Microsoft 扩展,我认为它已被弃用。使用 getElementById。

I think there's something wrong in the way you create your selectStr. You're gonna use it as innerHTML for a Select so:

我认为您创建 selectStr 的方式有问题。你将把它用作 Select 的innerHTML,所以:

  • it should NOT end with a </select>. The closing tag for an element is not part of its innerHTML afaik
  • you should have closing tags for your <option>elements
  • 它不应该以</select>. 元素的结束标记不是其innerHTML afaik的一部分
  • 你的<option>元素应该有结束标签

I'd change the following piece of code:

我会更改以下代码:

// checking some condition for freqValues.
for(var i=4;i<freqValues.length;i++)
        selectStr += '<option value="'+freqValues[i]+'">'+freqTexts[i]+'\n';
}
selectStr += '</select>';

into this:

进入这个:

// checking some condition for freqValues.
for(var i=4;i<freqValues.length;i++)
        selectStr += '<option value="'+freqValues[i]+'">'+freqTexts[i]+'</option>\n';
}

NOTE: I've read somewhere that using innerHTML on SELECT controls has a really browser-dependant behaviour, even when using correctly formatted code. I think a more robust solution would be using removeChild and addChild to dinamically remove and add options nodes .

注意:我在某处读到在 SELECT 控件上使用 innerHTML 具有真正依赖于浏览器的行为,即使使用格式正确的代码也是如此。我认为更强大的解决方案是使用 removeChild 和 addChild 动态删除和添加选项节点。

回答by vaquar khan

Issue is related to document mode , IE11 default document mode is IE7.

问题与文档模式有关,IE11 默认文档模式为 IE7。

Using F12 in IE Emulation you can change document mode 8,9 and 11

在 IE Emulation 中使用 F12 您可以更改文档模式 8,9 和 11

You can use following tag to change document mode using programming.

您可以使用以下标签通过编程更改文档模式。

For versions of Internet Explorer 8 and above, this:

对于 Internet Explorer 8 及更高版本,此:

<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7" />   

Forces the browser to render as that particular version's standards. It is not supported for IE7 and below.

强制浏览器呈现为该特定版本的标准。IE7 及以下不支持。

If you separate with semi-colon, it sets compatibility levels for different versions. For example:

如果用分号分隔,它会设置不同版本的兼容性级别。例如:

<meta http-equiv="X-UA-Compatible" content="IE=7; IE=9" />

Renders IE7 and IE8 as IE7, but IE9 as IE9. It allows for different levels of backwards compatibility. In real life, though, you should only chose one of the options:

将 IE7 和 IE8 呈现为 IE7,但将 IE9 呈现为 IE9。它允许不同级别的向后兼容性。但是,在现实生活中,您应该只选择以下选项之一:

<meta http-equiv="X-UA-Compatible" content="IE=8" />

This allows for much easier testing and maintenance. Although generally the more useful version of this is using Emulate:

这使得测试和维护更加容易。虽然通常更有用的版本是使用 Emulate:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />

For this:

为了这:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

It forces the browser the render at whatever the most recent version's standards are. Just like using the latest version of jQuery on Google's CDN, this is the most recent, but also can potentially break your code since its not a fixed version.

它强制浏览器以最新版本的标准进行渲染。就像在 Google 的 CDN 上使用最新版本的 jQuery 一样,这是最新版本,但也可能会破坏您的代码,因为它不是固定版本。

Last , consider adding this little tidbit:

最后,考虑添加这个小花絮:

<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

Adding "chrome=1" will allow the site to render in ChromeFrame for those (intelligent) users who have it, without affecting anyone else.

添加“chrome=1”将允许站点在 ChromeFrame 中为拥有它的(智能)用户呈现,而不会影响其他任何人。

`enter code here

`在此处输入代码

`https://technet.microsoft.com/en-us/itpro/internet-explorer/ie11-deploy-guide/fix-compat-issues-with-doc-modes-and-enterprise-mode-site-list

` https://technet.microsoft.com/en-us/itpro/internet-explorer/ie11-deploy-guide/fix-compat-issues-with-doc-modes-and-enterprise-mode-site-list

https://msdn.microsoft.com/library/ms533876(v=vs.85).aspx

http://www.chromium.org/developers/how-tos/chrome-frame-getting-started