限制从复选框列表中选择的选项数

Limit Number of Options Selected From CheckboxList

我试图只允许用户从可能有 12 个选项的复选框列表中选择 select 5 个选项。我已经添加了这段代码,但用户可以 select 随心所欲。

这是我的完整语法。

为什么这没有像我希望的那样发出警报?

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
       <script type="text/javascript">
        var limit = 5;
        $(function () { 
        $('[id*="cbxlTestInputAmt"]').on('change', function (evt) {
            if ($('[id*="cbxlTestInputAmt"]:checked').length > limit) {
                this.checked = false;
                alert('cannot select more than ' + limit);
            }
        });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:CheckBoxList ID="cbxlTestInputAmt" runat="server" RepeatLayout="table" RepeatColumns="2" RepeatDirection="vertical"></asp:CheckBoxList>    
    </div>
    </form>
</body>
</html>

我正在从数据库后面的 C# 代码中填充我的复选框列表。我认为这就是 JQuery 对我不起作用的原因。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLString1"].ConnectionString))
        {
            SqlDataAdapter ad = new SqlDataAdapter("Select X FROM OIKJC ORDER BY ID ASC", conn);
            ad.Fill(dt);
        }
        cbxlTestInputAmt.DataSource = dt;
        cbxlTestInputAmt.DataTextField = "X";
        cbxlTestInputAmt.DataValueField = "X";
        cbxlTestInputAmt.DataBind();
    }
}

编辑 2
这是页面源代码

    <!DOCTYPE html>

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

</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <form method="post" action="./StJudes" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/arN4mVK5SbGdvL+R7WrYMRnz8V5l4JL9V3Mni0wMrfoNV8HdxGgfuGLcsQDI0QcQeB6J8U7mUzXSzGMKyLlJ/aalb0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UUHotDlWD8bYbntUAlTVhJ1e5amY7RgzWNVPhlsTLIfFF5vwQAW0Qg9BSUTKOtx1A5dssTY9iUlVa+gJDjfEohvLcO9Qtym8As+OA2tBCJ/2jQyhGgo+prm1S+MVGTVuItm" />
</div>

    <div>    
        <table id="cbxlTestInputAmt">
    <tr>
        <td><input id="cbxlTestInputAmt_0" type="checkbox" name="cbxlTestInputAmt[=12=]" value="Choice 1" /><label for="cbxlTestInputAmt_0">Choice 1</label></td><td><input id="cbxlTestInputAmt_8" type="checkbox" name="cbxlTestInputAmt" value="Choice 5" /><label for="cbxlTestInputAmt_8">Choice 5</label></td>
    </tr><tr>
        <td><input id="cbxlTestInputAmt_1" type="checkbox" name="cbxlTestInputAmt" value="Choice 2" /><label for="cbxlTestInputAmt_1">Choice 2</label></td><td><input id="cbxlTestInputAmt_9" type="checkbox" name="cbxlTestInputAmt" value="Choice 6" /><label for="cbxlTestInputAmt_9">Choice 6</label></td>
    </tr><tr>
        <td><input id="cbxlTestInputAmt_2" type="checkbox" name="cbxlTestInputAmt" value="Choice 3" /><label for="cbxlTestInputAmt_2">Choice 3</label></td><td><input id="cbxlTestInputAmt_10" type="checkbox" name="cbxlTestInputAmt" value="Choice 7" /><label for="cbxlTestInputAmt_10">Choice 7</label></td>
    </tr><tr>
        <td><input id="cbxlTestInputAmt_3" type="checkbox" name="cbxlTestInputAmt" value="Choice 4" /><label for="cbxlTestInputAmt_3">Choice 4</label></td><td><input id="cbxlTestInputAmt_11" type="checkbox" name="cbxlTestInputAmt" value="Choice 8" /><label for="cbxlTestInputAmt_11">Choice 8</label></td>
    </tr><tr>
    </tr>
</table>    
    </div>

<div class="aspNetHidden">

    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="EAAE48E8" />
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="Sy90TVaOKW0SPZaQc4kBfWFelyOefK2WoFykp7mXwf8t1tCQ1rpzVdnpgLuNxUsSq4LLnzDYvh5HpOvi781XrWO2QN3jsMS0nwr+2o8Qq5CSde1phnI9bO5cNFwJLytC54FG6o/Uv4AbjSKIvZ5hsmWRfu0AtPCuFsgeJXK3nkJI++SHHLuvdqc3dkJYCyzFY8ls6ton28qjRu1LfvTN40WIGxqe+XODIS5UEeFpj3eAbjN4vZnO9rAkE3mGOscT50S201BcFZQNRvzJMEDYe2lXadzzxCWP2P5456Q18+P3A9DPlZheRW6yTNdiUM1HUoZXpMjtR0+8nbnDnCBv4TNcjSdUCKz2hOWuuESysZr5u52LKxZFKQKXFpQVwLjm4GiNRSs03ERpdikPXXMH1Wg1j8SrK5eYkZiGO+DMBciiv3nud//ZiRZeeUcn14Mj" />
</div></form>
    <script type="text/javascript">
           var limit = 5;
           $(function () {
               $('#form1').on('change', '[id="cbxlTestInputAmt"]', function (evt) {
                   if ($('[id="cbxlTestInputAmt"]:checked').length > limit) {
                       this.checked = false;
                       alert('cannot select more than ' + limit);
                   }
               });
           });
    </script>

<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
    {"appName":"Chrome","requestId":"fc04a78059374a078423b035a01ea2a7"}
</script>
<script type="text/javascript" src="http://localhost:60326/3217fe137445440cbce21c780fe62311/browserLink" async="async"></script>
<!-- End Browser Link -->

</body>
</html>

尽管您发布的服务器端代码指示带有 *= 限定符的属性选择器,但您发布的渲染源代码显示了两个正在检查相等性的 id 选择器:

[id="cbxlTestInputAmt"]

当我将它们更改为:

[id*="cbxlTestInputAmt"]

代码正常工作。

<!DOCTYPE html>

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

</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <form method="post" action="./StJudes" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/arN4mVK5SbGdvL+R7WrYMRnz8V5l4JL9V3Mni0wMrfoNV8HdxGgfuGLcsQDI0QcQeB6J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KZsDdN6foV7iBPH5TABi2r8SUUHotDlWD8bYbntUAlTVhJ1e5amY7RgzWNVPhlsTLIfFF5vwQAW0Qg9BSUTKOtx1A5dssTY9iUlVa+gJDjfEohvLcO9Qtym8As+OA2tBCJ/2jQyhGgo+prm1S+MVGTVuItm" />
</div>

    <div>    
        <table id="cbxlTestInputAmt">
    <tr>
        <td><input id="cbxlTestInputAmt_0" type="checkbox" name="cbxlTestInputAmt[=12=]" value="Choice 1" /><label for="cbxlTestInputAmt_0">Choice 1</label></td><td><input id="cbxlTestInputAmt_8" type="checkbox" name="cbxlTestInputAmt" value="Choice 5" /><label for="cbxlTestInputAmt_8">Choice 5</label></td>
    </tr><tr>
        <td><input id="cbxlTestInputAmt_1" type="checkbox" name="cbxlTestInputAmt" value="Choice 2" /><label for="cbxlTestInputAmt_1">Choice 2</label></td><td><input id="cbxlTestInputAmt_9" type="checkbox" name="cbxlTestInputAmt" value="Choice 6" /><label for="cbxlTestInputAmt_9">Choice 6</label></td>
    </tr><tr>
        <td><input id="cbxlTestInputAmt_2" type="checkbox" name="cbxlTestInputAmt" value="Choice 3" /><label for="cbxlTestInputAmt_2">Choice 3</label></td><td><input id="cbxlTestInputAmt_10" type="checkbox" name="cbxlTestInputAmt" value="Choice 7" /><label for="cbxlTestInputAmt_10">Choice 7</label></td>
    </tr><tr>
        <td><input id="cbxlTestInputAmt_3" type="checkbox" name="cbxlTestInputAmt" value="Choice 4" /><label for="cbxlTestInputAmt_3">Choice 4</label></td><td><input id="cbxlTestInputAmt_11" type="checkbox" name="cbxlTestInputAmt" value="Choice 8" /><label for="cbxlTestInputAmt_11">Choice 8</label></td>
    </tr><tr>
    </tr>
</table>    
    </div>

<div class="aspNetHidden">

    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="EAAE48E8" />
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="Sy90TVaOKW0SPZaQc4kBfWFelyOefK2WoFykp7mXwf8t1tCQ1rpzVdnpgLuNxUsSq4LLnzDYvh5HpOvi781XrWO2QN3jsMS0nwr+2o8Qq5CSde1phnI9bO5cNFwJLytC54FG6o/Uv4AbjSKIvZ5hsmWRfu0AtPCuFsgeJXK3nkJI++SHHLuvdqc3dkJYCyzFY8ls6ton28qjRu1LfvTN40WIGxqe+XODIS5UEeFpj3eAbjN4vZnO9rAkE3mGOscT50S201BcFZQNRvzJMEDYe2lXadzzxCWP2P5456Q18+P3A9DPlZheRW6yTNdiUM1HUoZXpMjtR0+8nbnDnCBv4TNcjSdUCKz2hOWuuESysZr5u52LKxZFKQKXFpQVwLjm4GiNRSs03ERpdikPXXMH1Wg1j8SrK5eYkZiGO+DMBciiv3nud//ZiRZeeUcn14Mj" />
</div></form>
    <script type="text/javascript">
           var limit = 5;
           $(function () {
               $('#form1').on('change', '[id*="cbxlTestInputAmt"]', function (evt) {
                   if ($('[id*="cbxlTestInputAmt"]:checked').length > limit) {
                       this.checked = false;
                       alert('cannot select more than ' + limit);
                   }
               });
           });
    </script>

<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
    {"appName":"Chrome","requestId":"fc04a78059374a078423b035a01ea2a7"}
</script>
<script type="text/javascript" src="http://localhost:60326/3217fe137445440cbce21c780fe62311/browserLink" async="async"></script>
<!-- End Browser Link -->

</body>
</html>