首页 > 解决方案 > JavaScript 确认 OK 按钮不调用服务器端方法

问题描述

确认好的按钮不起作用,它没有调用服务器端方法。

按钮被禁用,但未调用服务器端功能。

如果我从确认方法中删除以下两行,那么它工作正常;

      document.getElementById('btnSubmit').disabled = true;
      document.getElementById('btnCancel').disabled = true;

代码

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script language="javascript">

    function confirmPayment() {

        var isOkay = confirm("Confirm Payment?");

        if (isOkay) {
            document.getElementById('btnSubmit').disabled = true;
            document.getElementById('btnCancel').disabled = true;
         
            return isOkay;
        } else {
            return isOkay;
        }
    }

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="btnSubmit" runat="server"  Width="110px" OnClientClick="return confirmPayment();" 
                Text="Submit Payment" ></asp:Button>
    <asp:Button ID="btnCancel" runat="server"  Width="110px"
                Text="Cancel" CausesValidation="False"></asp:Button>
</div>
</form>

</body>

有什么想法吗 ?

标签: javascriptasp.netwebforms

解决方案


我建议您设置控件 ClientIDMode="Static" 以防万一。

如果你不这样做,那么你应该采用这种格式:

var btn = document.getElementById('<%=Button1.ClientID%>');

或者在你的情况下:

document.getElementById('<%=btnSubmit.ClientID%>).disabled = true;

推荐阅读