简体   繁体   English

使用javascript从gridview中的选中复选框获取价值

[英]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.. 我有gridview控件,其中列包含复选框以选择一行,并且标题模板中还有另一个复选框以选择所有行。

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. 现在我想使用javascript从选中的复选框(所有选中的复选框或可能是4到5个选中的复选框)中获取值,并且当用户取消选中选中的复选框时,其值不应传递给javascript函数或为空。

 <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. 我建议您将jQuery用于此任务。

You can see how to get checkbox value here: http://jquery-howto.blogspot.ru/2008/12/how-to-check-if-checkbox-is-checked.html 您可以在此处查看如何获取复选框值: 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 在页面以HTML格式呈现给客户端之后,这将为您的页面生成许多复选框元素

using jQuery you can get the value same as the following 使用jQuery,您可以获得与以下相同的值

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

replace i with the index of the element you want 将i替换为所需元素的索引

another solution is by adding rel="chk<% #Eval(id) %>" to the check-box 另一种解决方案是将rel =“ chk <%#Eval(id)%>”添加到复选框

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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