在中继器中只有一个复选框被选中

本文关键字:复选框 中继器 有一个 | 更新日期: 2023-09-27 18:08:12

我有一个重复器,每个项目有一个复选框。我想一次只检查一个。

复选框是asp:checkbox,不知道有什么区别。我尝试了一些jquery,但它似乎不起作用。

 <asp:Repeater runat="server" DataSourceID="SqlDataSource1" ID="repeater1">
  <ItemTemplate>
  <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" Checked='<%# Eval("carousel_check")%>'/>
 </ItemTemplate>
 </asp:Repeater>

我已经尝试给复选框一个cssclass (checked_button),然后这个脚本。

    $(document).ready(function () {
        $('.checked_button').change(function () {
            if ($(this).is(':checked')) {
                $('.radio-similar').not(this).removeAttr('checked');
            };
        });
    });

在中继器中只有一个复选框被选中

Net复选框在span标签内呈现。

<span class="checked_button">
   <input id="MainContent_Repeater1_CheckBox1_2" 
     type="checkbox" name="ctl00$MainContent$Repeater1$ctl02$CheckBox1">
</span>

所以选择器应该是.checked_button input。然后取消选中所有复选框,只选中触发click事件的复选框。

$(function() {
    $(".checked_button input").click(function () {
        $('.checked_button input').attr("checked", false);
        $(this).prop("checked", true);
     });
});