单击后,我使用以下Javascript禁用按钮:

var c = 0;
function DisableClick(target) {
    var objName = target;
    document.getElementById(objName).disabled = true;
    c = c + 1;
    msg = 'Please Wait...(' + c + ')!';
    document.getElementById(objName).value = msg;
    var t = setTimeout('DisableClick()', 1000);
}

<asp:Button ID="btnLogin" runat="server" CssClass="cssLoginButton blue"  Text="Log in" ToolTip="Log in" ValidationGroup="UserLogin" onclick="btnLogin_Click" OnClientClick="DisableClick('btnLogin')" />


我的Javascript出现此错误:


  Microsoft JScript运行时错误:无法设置属性“ disabled”的值:对象为null或未定义




我该如何解决?

最佳答案

您可以在javascript函数中传递被单击的按钮对象。如果不需要回发,则返回false。

更改

OnClientClick="DisableClick('btnLogin')"




// the DisableClick make a loop, so make the return here.
OnClientClick="DisableClick(this);return false;"


function DisableClick(target) {
    target.disabled = true;
    c = c + 1;
    msg = 'Please Wait...(' + c + ')!';
    target.value = msg;

    // The DisableClick needs the target parametre, so send it again
    //  but need to keep it here for work.
    var me = target;
    var t = setTimeout(function(){DisableClick(me);}, 1000);
}

10-06 04:00