选中并取消选中标题复选框 [英] Check and uncheck header checkbox

查看:106
本文介绍了选中并取消选中标题复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 场景



标题复选框就在那里..



1)如果我检查了标题复选框,,,所有子复选框都将被选中

2)如果取消选中所有将被取消选中



3)如果一切都是检查..如果我取消选中一个孩子复选框...标题复选框将取消选中

4)如果我逐个检查所有子复选框....标题复选框将被选中。





1,2和3工作正常。第四个不工作

chkb1是标题复选框

chkb2是子复选框

< script type =text /的javascript> 
$(function(){

$('[id * = chkb1]')。click(function(){
$([id * ='chkb2'' ])。attr(checked,this.checked);
});
$('[id * = chkb2]')。click(function(){
if( $('[id * = chkb2]')。length == $([id * ='chkb2']:checked)。length)
{
$([id * =' chkb1'])。attr(checked,this.checked);
}
else
{
$([id * ='chkb1'])。 removeAttr(checked);
}
});
});< / script>





---- gridview有复选框



 <   asp:GridView     ID   =  GridZeroApprovedKm     AllowPaging   = < span class =code-keyword> true    runat   =  server    AutoGenerateColumns   =  false  

DataKeyNames = Address_id OnRowCancelingEdit = GridZeroApprovedKm_RowCancelingEdit

< span class =code-attribute> OnRowDataBound
= GridZeroApprovedKm_RowDataBound OnRowDeleting = GridZeroApprovedKm_RowDeleting

< span class =code-attribute> OnRowEditing = GridZeroApprovedKm_RowEditing

OnRowUpdating = GridZeroApprovedKm_RowUpdating

onpageindexchanging = GridZeroApprovedKm_PageIndexChanging >
< SelectedRowStyle CssClass = ResultSelectedR ow BackColor = 青色 / >
< >
< asp:TemplateField HeaderText = 全选 >
< HeaderTemplate >
< <跨班=code-leadattribute> asp:CheckBox
ID = chkb1 runat = server 文字 = 全选 OnCheckedChanged = sellectAll

< span class =code-attribute> AutoPostBack = true / >
< / HeaderTemplate < span class =code-keyword>>
< ItemStyle Horizo​​ntalAlign = 中心 VerticalAlign = 中间 / &g t;
< ItemTemplate >
< asp:CheckBox ID = chkb2 runat = server OnCheckedChanged = sellectAllChildcheckbox 文字 = 选择 AutoPostBack = true / >
< ; / ItemTemplate >
< ItemStyle Horizo​​ntalAlign = center / >
< / asp:TemplateField >







---这种语法不正确



if($('[id * = chkb2]')。length == $([id * ='chkb2']:checked)。length )

解决方案

(function(){


('[id * = chkb1]')。click(function() {

( [ID * = 'chkb2'])ATTR( 选中,this.checked)。
});


--scenario

header checkbox is there..

1)if i check the header check box ,,,all the child checkbox will be checked
2)if uncheck all will be unchecked

3)if everything is checked..if i uncheck one child checkbox...header checkbox will be unchecked
4)if i check one by one all the child checkbox....header checkbox will be checked.


1,2 and 3 are working fine. 4th is not working
chkb1 is header checkbox
chkb2 is child checkbox

<script type="text/javascript">
    $(function(){
    
            $('[id*=chkb1]').click(function(){
            $("[id*='chkb2']").attr("checked",this.checked);
             });  
                    $('[id*=chkb2]').click(function(){
                    if($('[id*=chkb2]').length== $("[id*='chkb2']: checked").length)
                     {                    
                     $("[id*='chkb1']").attr("checked",this.checked);                     
                     }
                     else
                     {                   
                      $("[id*='chkb1']").removeAttr("checked");                     
                     }           
                    }); 
    });</script>



----gridview having checkbox

<asp:GridView ID="GridZeroApprovedKm" AllowPaging="true" runat="server" AutoGenerateColumns="false"

                   DataKeyNames="Address_id" OnRowCancelingEdit="GridZeroApprovedKm_RowCancelingEdit"

                   OnRowDataBound="GridZeroApprovedKm_RowDataBound" OnRowDeleting="GridZeroApprovedKm_RowDeleting"

                   OnRowEditing="GridZeroApprovedKm_RowEditing"

                   OnRowUpdating="GridZeroApprovedKm_RowUpdating"

                   onpageindexchanging="GridZeroApprovedKm_PageIndexChanging">
                   <SelectedRowStyle CssClass="ResultSelectedRow" BackColor="Cyan" />
                   <Columns>
                       <asp:TemplateField HeaderText="Select All">
                           <HeaderTemplate>
                               <asp:CheckBox ID="chkb1" runat="server" Text="Select All" OnCheckedChanged="sellectAll"

                                   AutoPostBack="true" />
                           </HeaderTemplate>
                           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                           <ItemTemplate>
                               <asp:CheckBox ID="chkb2" runat="server" OnCheckedChanged="sellectAllChildcheckbox" Text="Select" AutoPostBack="true" />
                           </ItemTemplate>
                           <ItemStyle HorizontalAlign="center" />
                       </asp:TemplateField>




---is this syntax incorrect

if($('[id*=chkb2]').length== $("[id*='chkb2']: checked").length)

解决方案

(function(){


('[id*=chkb1]').click(function(){


("[id*='chkb2']").attr("checked",this.checked); });


这篇关于选中并取消选中标题复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆