基于asp.net复选框的启用和禁用按钮控件的问题

Problem with enabling and disabling button control based on the asp.net check box check

本文关键字:按钮 控件 问题 启用 asp net 复选框 基于      更新时间:2023-09-26

嗨,朋友们,我有一个按钮控件,必须基于我的asp.net复选框复选启用,但是当运行代码时,我面临的问题是,我的按钮仍然被禁用,即使我执行复选框复选。是他们的任何属性,我已经设置在代码后面访问检查事件。

我在我的应用程序中使用以下代码这是我的javascript文件

<script type="text/javascript">
                    function theChecker() {
                        var checkboxId = '<%=  SpEligible.ClientID %>';
                        alert(checkboxId);
                        if (checkboxId.checked == true)
                             {
                            document.getElementById("SplistButton").disabled = false;
                             }
                             else
                              {
                            document.getElementById("SplistButton").disabled = true;
                              }
                            }
</script>

这是我的代码复选框和按钮是

<asp:CheckBox ID="SpEligible" runat="server" Text="SpEligible" class="cBox"   />
   <asp:Button ID="SplistButton" runat="server" OnClientClick=" return ShowInsertForm()"    Enabled="false"/>

这是我的aspx.cs文件,我调用javascript

SpEligible.Attributes.Add("onclick", "theChecker()");

我可以看到你的代码中的两个主要错误:

1-在您的<script>标记中,您确实意识到您的复选框在页面中不会有相同的ID,但您没有进行该检查。此外,正如Ken Pespisa所提到的,您采取的ID只是一个字符串,因此,它不知道任何checked属性。下面是我要写的代码:

<script type="text/javascript">
    function theChecker() {
        var checkboxId = '<%=  SpEligible.ClientID %>';
        alert(checkboxId);
        if (document.getElementById(checkboxId).checked == true)
        {
            document.getElementById("<%=  SplistButton.ClientID %>").disabled = false;
        }
        else
        {
            document.getElementById("<%=  SplistButton.ClientID %>").disabled = true;
        }
    }
</script>

2-在您的.cs页面中,您似乎没有使用任何命名空间。你可能隐藏了一些代码,所以我只想说一定要有命名空间并且一定要在函数中使用这行,也许是页面加载事件函数

您需要将javascript代码更改为:

<script type="text/javascript">
                    function theChecker() {
                        var checkboxId = document.getElementById('<%=  SpEligible.ClientID %>');
                        alert(checkboxId);
                        if (checkboxId.checked == true)
                             {
                            document.getElementById("SplistButton").disabled = false;
                             }
                             else
                              {
                            document.getElementById("SplistButton").disabled = true;
                              }
                            }
</script>

您正在检查字符串常量的checked属性。您需要使用已检查属性的document.getElementById获得控件本身。我还将"checkboxId"重命名为"checkbox"

改变. getelementbyid (SplistButton)

. getelementbyid (checkboxId)