简体   繁体   中英

How to get selected values of checkbox list

I have a check box list

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

and bind as

ddrCustomer.DataSource = ds.Tables[1];
 ddrCustomer.DataTextField = "Customer";
 ddrCustomer.DataValueField = "Id";
 ddrCustomer.DataBind();
 ddrCustomer.Items.Insert(0, new ListItem("All", "All"));

then Rendered HTML is:

   <tr>
     <td>
          <input type="checkbox" name="ddrCustomer$1" id="ddrCustomer_1">
          <label for="ddrCustomer_1">10 Manhattan Ave, Storage</label>
     </td>
   </tr>

when i call below java script function

  $("#ddrCustomer").find("input[type=checkbox]:checked").each(function () {

      alert($(this).val());

    }); 

then it always return "on" as checked check boxes values. I need to bind valus

$("#ddrCustomer").find("input[type=checkbox]").each(function () {

alert($(this).val());

});

.aspx Markup

<asp:CheckBoxList ID="ddrCustomer" runat="server">
    <asp:ListItem Text="Sam" Value="1"></asp:ListItem>
</asp:CheckBoxList>

Rendered HTML

<input type="checkbox" value="1" name="ctl00$MainContent$ddrCustomer$0"  
               id="MainContent_ddrCustomer_0">
<label for="MainContent_ddrCustomer_0">Sam</label>

jQuery

$("#<%= ddrCustomer.ClientID %>").find("input[type=checkbox]").each(function () {

alert(this.checked); // returns checked or not
alert(this.value); //returns value
alert($(this).next('label').text(); // returns SAM. Text value

});

use:

 $("#ddrCustomer").find("input[type=checkbox]").each(function () {
  alert($(this).val());
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM