[英]Why do I get a "Cannot read property 'click' of null error
Inside the BODY
tag: 在
BODY
标签内:
<asp:Panel ID="nmSearch" CssClass="searchBox" runat="server" DefaultButton="HiddenSearchNM">
<input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />
<a href="JavaScript:void(0);" onclick="SearchNMClick();" title="Search" class="styledBtnSearch searchAnchor floatLeft defaultLinks">
<asp:Image ImageUrl="~/images/searchWhite.png" CssClass="searchImg" runat="server" ToolTip="Search" AlternateText="Search" />
</a>
<asp:ImageButton ID="HiddenSearchNM" runat="server" CssClass="hideContent" ClientIDMode="Static" />
</asp:Panel>
Inside the HEAD
tag: 在
HEAD
标签内:
<script>
function SearchNMClick() {
document.getElementById('HiddenSearchNM').click();
}
</script>
I see the following error in the console: 我在控制台中看到以下错误:
Uncaught TypeError: Cannot read property 'click' of null
SearchNMClick
onclick
C# code behind which will fire off a search page from either Enter
or click: 后面的C#代码将从
Enter
或单击触发搜索页面:
protected void HiddenSearchNM_Click(object sender, EventArgs e)
{
//MessageBox.Show("SEARCH NM");
strSMain = searchB.Value;
Response.Redirect("results.aspx?searchtext=" + strSMain +"&folderid=0&searchfor=all&orderby=title&orderdirection=ascending");
}
But when I hit enter while in the textbox or the button click, I get the error above. 但是当我在文本框中按Enter键或点击按钮时,我得到上面的错误。
How do I resolve the error. 我该如何解决错误。
So weird, when I check the HTML source I see this (not sure why the ID is being changed): 太奇怪了,当我检查HTML源代码时,我看到了这一点(不确定为什么要更改ID):
<input type="image" name="ctl00$CUSTOM_Area_Top$HiddenSearchNM" id="ctl00_CUSTOM_Area_Top_HiddenSearchNM" class="hideContent" ClientIDMode="Static" src="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$CUSTOM_Area_Top$HiddenSearchNM", "", true, "", "", false, false))" style="border-width:0px;" />
Use a Page
directive to set static ids 使用
Page
指令设置静态ID
<%@ Page ClientIDMode="static" %>
That should work with your id selector. 这应该适用于你的id选择器。
Also, check your rendered HTML for your ImageButton
. 另外,检查您的
ImageButton
渲染HTML。 If everything is setup correctly, it should preserve the ID
. 如果一切都设置正确,它应该保留
ID
。
You may change you JS function to: 您可以将JS功能更改为:
function SearchNMClick() {
document.getElementById('<%# HiddenSearchNM.ClientID %>').click();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.