Search code examples
javascriptc#asp.netcheckboxupdatepanel

Get the id of the checked input checkboxes and add to a list


I'll start off saying, i know hardly anything about java script and i have a feeling this requires it. What i would like to do is find out which checkboxes are checked, add those to a list and pass that to the code behind page.

This is how i create the checkboxes from the results i get.

<table width="100%" border="1">
     <%foreach (var qv in rs)
     {
         var checkid = "chk" + qv.id;
         var tdid = "td" + qv.id;
         var text = qv.text.ToString();                                          
      %>
      <tr >
           <td width="100%">
           <input type="checkbox" id="<%=qv.id%>" value="@qv.id" />
           </td>
           <td width="100%">
           <%=qv.text%>
           </td>
      </tr>
      <%                                      
      } %>
</table> 

Can anyone help me?

So I found exactly what I'm looking for but when I use the code, nothing works, even when I copy paste it exactly it doesnt work, am I missing something silly?

Here is the link to the working demo: http://jsfiddle.net/dvCmR/ and here is my code, exactly the same but not working.

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="assets/scripts.js"></script>
<script type="text/javascript">
    $("#clickme").click(function (e) {
        var selected = $("#checkboxes input:checked").map(function (i, el) { return el.name; }).get();
        alert("selected = [" + selected + "]\nas string = \"" + selected.join(";") + "\"");
    });
</script>
<div id="checkboxes">
    <input id="chkbx_0" type="checkbox" name="c_n_0" checked="checked" />Option 1
    <input id="chkbx_1" type="checkbox" name="c_n_1" />Option 2
    <input id="chkbx_2" type="checkbox" name="c_n_2" />Option 3
    <input id="chkbx_3" type="checkbox" name="c_n_3" checked="checked" />Option 4
</div>
<input type="button" id="clickme" value="click me, now!" />


Solution

  • $checkedBoxeIds;
    $('table').each(function (i, row) {
    var $row = $(row),
    $checkedBoxe = $row.find('input:checked')[0];
    $checkedBoxeIds += $checkedBoxeIds +";"
    });
    $.ajax({
    type: "POST", url: myUrl,data: { checkedIs : $checkedBoxeIds},
    success: function(data) {
    alert('it worked');
    }});