2010-12-14 55 views
0

iam使用gridview中的複選框。我想檢查並取消選中使用jQuery的GridView中的複選框。我用現場方法試了一下。它在gridview中的第一頁工作,但不在頁面索引更改事件中。使用jQuery檢查並取消選中Asp.net GridView中的複選框

$(document).ready(function() { 

     var checkBoxSelector = '#<%=grv_ClientList.ClientID%> input[id*="chck_itemSelect"]:checkbox'; 

     //header checkbox 
     $('[id$=chck_headSelect]').live("click", function() { 

      if ($(this).is(":checked")) { 

       $(checkBoxSelector).attr('checked', true); 

      } 
      else { 

       $(checkBoxSelector).attr('checked', false); 
      } 
     }); 

    }); 

回答

0

請仔細閱讀示例代碼。

<script type="text/javascript"> 
function CheckUnCheckAll(chk) { 
$('#<%=GridView1.ClientID %>').find("input:checkbox").each(function() { 
    if (this != chk) { 
     this.checked = chk.checked; 
    } 
    }); 
    } 
</script> 

檢查例如:

<script src="jquery-1.4.1.js" type="text/javascript"></script> 

<script src="jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 



<script type="text/javascript" > 

    $(document).ready(function() { 

    var ab = 0 ; 

     $("[id$=myCheck]").click(function() { 



      if (ab == 0) { 

       $('#<%=GridView1.ClientID %> >tbody >tr >td:first-child > input:checkbox').attr('checked', true); 

       ab = 1; 

      } 

      else { 

       $('#<%=GridView1.ClientID %> >tbody >tr >td:first-child > input:checkbox').attr('checked', false); 

       ab =0 ; 

      } 



     }) 

    }) 



</script> 

 <Columns> 



      <asp:TemplateField> 

      <HeaderTemplate> 

      <asp:CheckBox ID="myCheck" runat="server" /> 

      </HeaderTemplate> 

      <ItemTemplate><asp:CheckBox ID="urCheck" runat="server" /></ItemTemplate> 



      </asp:TemplateField> 





      <asp:BoundField DataField="UnitName" HeaderText="Unit Name" /> 

      <asp:BoundField DataField="Description" HeaderText="Description" /> 

      <asp:TemplateField HeaderText="Status"> 

      <ItemTemplate> 



      <asp:DropDownList ID="AttendId" runat="server" > 

      <asp:ListItem style="Color:Green" Text="Present" Value="0"></asp:ListItem> 

      <asp:ListItem style="Color:Red" Text="Absent" Value="1"></asp:ListItem> 

      <asp:ListItem style="Color:Blue" Text="Leave" Value="2"></asp:ListItem> 



      </asp:DropDownList> 



      </ItemTemplate>    

      </asp:TemplateField> 

     </Columns> 

    </asp:GridView> 
+0

我要保留選中和未選中狀態在gridview中的不同頁面之間。 – 2010-12-14 06:12:41

+0

我不確定,但在我看來,如果您必須保留選中的項目,應該使用ajax。 – Maxymus 2010-12-14 06:15:35

相關問題