JavaScript 运行时错误:无法获取未定义或空引用的属性“值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29968078/
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
JavaScript runtime error: Unable to get property 'value' of undefined or null reference
提问by Thamires Cunha
I wrote a javascript code to display the next date of a specific calendar
我写了一个 javascript 代码来显示特定日历的下一个日期
My Function
我的功能
function verificaDataReferencia(mensagem)
{
if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)
var objtxtDataReferencia1Hidden = document.getElementById("txtDataReferencia1Hidden").value;
var objtxtDataReferencia2Hidden = document.getElementById("txtDataReferencia2Hidden").value;
> Breakpoint var objtxtDataArquivo = document.getElementById("ddlDataPub").value;
var mensagem = document.getElementById("txtDataRefInfo").value;
if((objtxtDataReferencia1Hidden == objtxtDataArquivo) || (objtxtDataReferencia2Hidden == objtxtDataArquivo))
{
var x = alert(mensagem);
return x;
}
}
Html
html
<asp:TextBox style="Z-INDEX: 112; POSITION: absolute; TOP: 9px; LEFT: 572px" id="txtDataReferencia1Hidden"
runat="server" CssClass="inputLabel" Width="15" Height="15px"></asp:TextBox>
<asp:TextBox style="Z-INDEX: 113; POSITION: absolute; TOP: 9px; LEFT: 606px" id="txtDataReferencia2Hidden"
runat="server" CssClass="inputLabel" Width="14" Height="14px"></asp:TextBox>
the problem is that the page is running well in chrome but when i run my application in IE it throw an error
问题是该页面在 chrome 中运行良好,但是当我在 IE 中运行我的应用程序时,它抛出一个错误
0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference
0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“值”
please help me to overcome from this problem.
请帮助我克服这个问题。
private void Page_Load(object sender, System.EventArgs e)
{
if(! IsPostBack)
{
txtNomePortugues.SetFocus();
BO.Pasta pasta = new BO.Pasta(CodPasta);
if (CodigoArquivo == 0 && !IsJustificativa && pasta.EnviaEmail)
btnGravar.Attributes.Add("onclick", "verificaDataReferencia(); confirmaEnvioEmail('" + GetString("Mensagem_Confirmacao_Alerta_Arquivos") + "');");
else
btnGravar.Attributes.Add("onclick", "verificaDataReferencia();");
回答by Rahul Tripathi
The issue is here:
问题在这里:
var mensagem = document.getElementById("txtDataRefInfo").value;
var objtxtDataReferencia1Hidden = document.getElementById("txtDataReferencia1Hidden").value;
var objtxtDataReferencia2Hidden = document.getElementById("txtDataReferencia2Hidden").value;
Reason being txtDataRefInfo, txtDataReferencia1Hidden, txtDataReferencia2Hiddenis a server side control and not the client side. Try this:
原因是txtDataRefInfo,txtDataReferencia1Hidden,txtDataReferencia2Hidden是一个服务器端的控制,而不是客户端。尝试这个:
var objtxtDataReferencia1Hidden = document.getElementById('<%=txtDataReferencia1Hidden.ClientID%>');
var objtxtDataReferencia2Hidden = document.getElementById('<%=txtDataReferencia2Hidden.ClientID%>');
var mensagem = document.getElementById('<%=txtDataRefInfo.ClientID%>');
回答by Jerome
You need to set the ClientIDMode attribute on the control to:
您需要将控件上的 ClientIDMode 属性设置为:
ClientIDMode="Static"
This works with .Net 4.0 and upwards
这适用于 .Net 4.0 及更高版本
回答by MacKentoch
comment this line for IE it may be ok :
为 IE 注释此行可能没问题:
// > Breakpoint var objtxtDataArquivo = document.getElementById("ddlDataPub").value;
回答by Vizllx
The problem is that txtDataReferencia1Hidden and txtDataReferencia2Hidden are the server name of controls, not the client name (look the source of page from the browser).
问题是 txtDataReferencia1Hidden 和 txtDataReferencia2Hidden 是控件的服务器名称,而不是客户端名称(从浏览器查看页面源)。
Use this:
用这个:
var objtxtDataReferencia1Hidden = document.getElementById('<%=txtDataReferencia1Hidden.ClientID%>');
var objtxtDataReferencia2Hidden = document.getElementById('<%=txtDataReferencia2Hidden.ClientID%>');
回答by Pavel Gatnar
there is missing an action behind this if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)
这背后缺少一个动作 if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)
I would expect you write the line in this way (do not forget the negation using !):
我希望你以这种方式写这行(不要忘记使用!):
if(!document.getElementById("txtDataReferencia1Hidden") || !document.getElementById("txtDataReferencia2Hidden") || !document.getElementById("ddlDataPub") || !document.getElementById("txtDataRefInfo")) return;
if(!document.getElementById("txtDataReferencia1Hidden") || !document.getElementById("txtDataReferencia2Hidden") || !document.getElementById("ddlDataPub") || !document.getElementById("txtDataRefInfo")) return;
回答by mac
You are checking to see if the elements are null in your if statement -
您正在检查 if 语句中的元素是否为空 -
if(document.getElementById("txtDataReferencia1Hidden") == null || document.getElementById("txtDataReferencia2Hidden") == null || document.getElementById("ddlDataPub") == null || document.getElementById("txtDataRefInfo") == null)
But even if they are null you are trying to get the value. Ex:
但即使它们为空,您也正在尝试获取该值。前任:
var mensagem = document.getElementById("txtDataRefInfo").value;
So if "txtDataRefInfo" is null the statement will return true and try to get the value anyways. Hence the undefined or null reference.
因此,如果“txtDataRefInfo”为空,则该语句将返回 true 并尝试获取该值。因此,未定义或空引用。

