为什么我得到一个“无法读取属性”点击'null错误 [英] Why do I get a "Cannot read property 'click' of null error

查看:65
本文介绍了为什么我得到一个“无法读取属性”点击'null错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

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#代码将从 Enter 中触发搜索页面或点击:

C# code behind which will fire off a search page from either Enter or click:

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");
    }

但是当我在文本框或点击按钮时按Enter键时,我得到了上面的错误。

But when I hit enter while in the textbox or the button click, I get the error above.

如何解决错误。

这很奇怪,当我查看HTML源代码时我看到了这个(不确定为什么要更改ID):

So weird, when I check the HTML source I see this (not sure why the ID is being changed):

<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(&quot;ctl00$CUSTOM_Area_Top$HiddenSearchNM&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" style="border-width:0px;" />


推荐答案

您可以将JS功能更改为:

You may change you JS function to:

function SearchNMClick() {
    document.getElementById('<%# HiddenSearchNM.ClientID %>').click();
}

这篇关于为什么我得到一个“无法读取属性”点击'null错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆