Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bind checkboxlist by jquery
my code is
ASP.NET
 <asp:CheckBoxList ID="chkListMY" runat="server" RepeatColumns="1">
  <asp:ListItem Text="" Value=""></asp:ListItem>
  </asp:CheckBoxList>

success: function (data)
    {if (data.length){
var repeatColumns = parseInt("<%=chkListMY.RepeatColumns%>");
 var cell = $("[id*=chkListMY] td").eq(0).clone(true);
  $("[id*=chkListMY] tr").remove();
for (var i = 0; i < data.length; i++)
  {
var row = $("<tr />");
$("[id*=chkListMY] tbody").append(row);
var checkbox = $("input[type=checkbox]", cell);                                                    //Set Unique Id to each CheckBox.
checkbox[0].id = checkbox[0].id.replace("0", i);                                                   //Give  text to each CheckBox.                        
checkbox[0].text = data[i].Text
//Set the CheckBox value.                                                  checkbox.val(data[i].Value);
var label = cell.find("label");
if (label.length == 0) {
label = $("<label />");
}
//Set the 'for' attribute of Label.
label.attr("for", checkbox[i].id);
//Set the text in Label.
label.html(data[i].Text);
//Append the Label to the cell.
cell.append(label);
//Append the cell to the Table Row.
row.append(cell);
cell = $("[id*=chkListMY] td").eq(0).clone(true);
};
}
}

JavaScript
<script type="text/javascript">
       
       
  
        $(document).ready(function () {       
            
  
getMonthYearCheckIndexChange();

</script>


function getMonthYearCheckIndexChange() {
            
            $("[id*=chkListMY] input[type=checkbox]").click(function () {
              
                var chks = $("[id*=chkListMY] input[type=checkbox]");
                var str = "";
                for (var i = 0; i < chks.length; i++) {
                    if (chks[i].checked) {
                        str = str + chks[i].text + ',';
                    }
                }
                str = str.substr(0, str.length - 1);
               alert(str);                
            });
        }

C#
it initially shows empty check box in checkboxlist but my requirement is ,it(empty check box) should not be show.
And When I checked any checkbox it is working means calling given function
Posted
Updated 27-Jan-16 23:27pm
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900