简体   繁体   中英

get value from checked checkbox inside a gridview using javascript

I have gridview control in which column contains checkboxes to select a row and i have also another checkbox in header template for select all rows purpose..

now i want to get value from selected check boxes (all selected or may be 4 to 5 selected checkboxes) using javascript and when user uncheck the selected check box its value should not pass to javascript function or should be empty.

 <asp:gridview id="gvFriends" runat="server" autogeneratecolumns="False">
    <columns>
        <asp:ImageField DataImageUrlField="PictureUrl" HeaderText="Picture" />
        <asp:BoundField DataField="Name" HeaderText="Name" />

        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="chkSelectAll" runat="server" Text="Select All" />
            </HeaderTemplate>
            <ItemTemplate>


  <input id="Checkbox1" type="checkbox" value='<%#Eval("id")%>' />
            </ItemTemplate>
        </asp:TemplateField>

    </columns>
</asp:gridview>

I recommend you to use jQuery for this task.

You can see how to get checkbox value here: http://jquery-howto.blogspot.ru/2008/12/how-to-check-if-checkbox-is-checked.html

This will generate many check-box elements to your page after it renders to the client as HTML

using jQuery you can get the value same as the following

$('<%= gvFriends.ClientName%>').find('input[type=checkbox]').eq(i).is(':checked')

replace i with the index of the element you want

another solution is by adding rel="chk<% #Eval(id) %>" to the check-box

$('<%= gvFriends.ClientName%>').find('input[rel=chk'+ id +']').is(':checked')

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