javascript 无法获取属性“click”的值:对象为空或未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9898817/
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-10-26 08:09:48  来源:igfitidea点击:

Unable to get value of the property 'click': object is null or undefined

c#javascriptasp.netonclick

提问by QuestionableSounder

So I want a Button1 to be clicked when the submit button has been clicked.

因此,我希望在单击提交按钮时单击 Button1。

Here is the relevant code:

这是相关的代码:

<input id="Submit1" type="submit" value="Search" onclick="javascript:check_user()" /> 
<script type="text/javascript">
            function check_user() {
                document.getElementById('<%=Button1.ClientID%>').click();
            }  
</script>
<asp:Button id="Button1" runat="server" Visible="false" OnClick="Button1_Click"/>

And here is the error I get:

这是我得到的错误:

Microsoft JScript runtime error: Unable to get value of the property 'click': object is null or undefined

Microsoft JScript 运行时错误:无法获取属性“click”的值:对象为空或未定义

Does anyone have any ideas as to what the problem might be? I've been tearing my hair out trying to work it out.

有没有人对可能出现的问题有任何想法?我一直在扯头发试图解决这个问题。

回答by COLD TOLD

if you are not limited to java-script you can try using the trigger from jquery like this

如果您不限于 java-script,您可以尝试使用 jquery 中的触发器,如下所示

 <script type="text/javascript">
            function check_user() {
           $("#<%=Button1.ClientID%>").trigger('click');
         //or

 $("#<%=Button1.ClientID%>").click();

            }  
</script>

回答by mellamokb

Can't you just do it with a single button?

你不能只用一个按钮来做吗?

<asp:Button id="Button1" runat="server" Text="Search" OnClick="Button1_Click"/>

Server-side code:

服务端代码:

protected void Button1_Click(object sender, EventArgs e) {
    // processing code here
}

回答by Chen G

As you made the button invisible, asp.net is not rendering the button control in html. (You can check the HTML source). You can create a style and apply it to the asp.net button once it is rendered at client side to make it invisible.

当您使按钮不可见时,asp.net 不会在 html 中呈现按钮控件。(您可以查看 HTML 源代码)。您可以创建一个样式并将其应用于在客户端呈现后的 asp.net 按钮以使其不可见。

.MyHiddenButtonStyle{display:none;}// add this in the header in styles

<input id="Submit1" type="button" name="search" value="Search" onclick="javascript:check_user()" />
<asp:Button id="Button1" runat="server" CssClass="MyHiddenButtonStyle" OnClick="Button1_Click"/>

function check_user() {
        document.getElementById('<%=Button1.ClientID%>').click();
    }

One more thing I have made the submit button as normal button. anyways you are raising a postback event using javascript.

还有一件事我将提交按钮设为普通按钮。无论如何,您正在使用 javascript 引发回发事件。