在页面加载中使用JavaScript禁用ASP.net按钮 [英] Disable ASP.net Button using javascript on pageload

查看:97
本文介绍了在页面加载中使用JavaScript禁用ASP.net按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在pageload上使用javascript禁用ASP.net按钮
我的代码如下
//================================================ ====

I need to disable an ASP.net button using javascript on pageload
my code as follows
//====================================================

<script type="text/javascript">
    function disableBtn()
    {
    var btn= document.getElementById("<%=Button1.ClientID%>");
    btn.disabled=true;
    }
    window.onload=disableBtn();
    </script>


//================================================ ==
< asp:Button ID ="Button1" runat =服务器" Text ="Button"/>
//================================================ =======
错误:Microsoft JScript运行时错误:"null"为null或不是对象


//==================================================
<asp:Button ID="Button1" runat="server" Text="Button" />
//=======================================================
Error: Microsoft JScript runtime error: ''null'' is null or not an object

推荐答案

那是因为您在加载窗口时调用了该函数并且Button元素当时不可用(因此为"null").

您可以做的是在身体负荷时调用该函数,它应该可以正常工作.因此代码将是
That''s because you have called the function on load of the window and the Button element is not available at that time (and thus ''null'').

What you can do is call the function on load of the body and it should work fine. So the code will be
<script type="text/javascript">
    function disableBtn()
    {
    var btn= document.getElementById("<%=Button1.ClientID%>");
    btn.disabled=true;
    }
    //window.onload=disableBtn(); //NOTE: I have commented this line
</script>


<body onload="javascript:disableBtn();">
...
...
<asp:Button ID="Button1" runat="server" Text="Button" />
...
</body>



希望这会有所帮助!



Hope this helps!


这篇关于在页面加载中使用JavaScript禁用ASP.net按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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