【问题标题】:validate checkboxlist in ASP.NET client side验证 ASP.NET 客户端中的复选框列表
【发布时间】:2014-04-05 23:16:36
【问题描述】:

我正在尝试使用以下代码验证我的复选框列表,但由于某种原因,它每次都会给我错误消息,即使选中了正确数量的复选框,而且我无法在任何地方找到任何 sulotions,任何人都可以发现我是什么做错了吗?

<asp:CheckBoxList ID="CheckBoxList" runat="server">
</asp:CheckBoxList>

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validate" ErrorMessage="choose a role, not more than 2"
        ValidationGroup="CreateUserWizard1"></asp:CustomValidator>

    <script type="text/javascript">
    function validate(source, arguments) {
        arguments.IsValid = false;

        var checklist = document.getElementById("CheckBoxList");
        if (checklist == null) return;

        var elements = checklist.getElementsByTagName("INPUT");
        if (elements == null) return;

        var checkBoxCount = 0;
        for (i = 0; i < elements.length; i++) {
            if (elements[i].checked) checkBoxCount++;
        }
        arguments.IsValid = (checkBoxCount > 0 || checkBoxCount <= 2);
    }
</script>

该脚本取自堆栈上的一个类似问题,我真的无法弄清楚“INPUT”与什么有关?

【问题讨论】:

    标签: javascript asp.net validation


    【解决方案1】:

    你必须得到你的复选框的渲染ID:

    var checklist = document.getElementById("&lt;%=CheckBoxList.ClientID%&gt;");

    您也可以阅读:Accessing control client name and not ID in ASP.NET

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多