交流,学习,进步!

千里之行始于足下……
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

跨页面实现多选

Posted on 2007-11-23 17:56  xixi8820  阅读(299)  评论(0编辑  收藏  举报

js:

 1function selectAllChk(chk)
 2        {
 3            //全选
 4            var obj = document.getElementsByTagName('input');
 5            for(var i = 0;i < obj.length;i++)
 6            {
 7                if (obj[i].type == 'checkbox' && obj[i].id.indexOf('chkID') > -1)
 8                {
 9                    obj[i].checked = chk;
10                    
11                    //全选赋值            
12                    if(obj[i].checked)//判断是否选中
13                    {
14                        if(Form2.txtIDs.value.indexOf(obj[i].value) > -1)//如果选择中,避免单选后再全选,造成ID重复
15                        {
16                            continue;
17                        }
18                        else
19                        {
20                                Form2.txtIDs.value += "," + obj[i].value; 
21                        }
22            
23                    }
24                    else
25                    {
26                        Form2.txtIDs.value = Form2.txtIDs.value.replace("," + obj[i].value,""); 
27                    }
28                }
29            }
30        }

 

 1function AddRemoveValues(oChk)
 2        {
 3            //单个选择赋值
 4            if(oChk.checked)
 5            {
 6                Form2.txtIDs.value += "," + oChk.value; 
 7            }
 8            else
 9            {
10                Form2.txtIDs.value = Form2.txtIDs.value.replace("," + oChk.value,""); 
11            }
12        }

 

 1<script type="text/javascript">
 2            //重新显示所选择的项目
 3            var obj = document.getElementsByTagName('input');
 4            for(var i = 0;i < obj.length;i++)
 5            {
 6                if (obj[i].type == 'checkbox' && obj[i].id.indexOf('chkID') > -1)
 7                {
 8                    if(Form2.txtIDs.value.indexOf(obj[i].value) >= 0)
 9                    {    
10                        obj[i].checked = true;
11                    }

12                    else
13                    {
14                        obj[i].checked = false;
15                    }

16                }

17            }

18            
</script>

隐藏控件用来保存ID:

1<input id="txtIDs" type="hidden" name="txtIDs" runat="server">


DataGrid 控件中的模版列:

 1<asp:TemplateColumn>
 2    <HeaderStyle Wrap="False" HorizontalAlign="Center" Width="5%"></HeaderStyle>
 3    <ItemStyle HorizontalAlign="Center"></ItemStyle>
 4    <HeaderTemplate>
 5<INPUT id="chkSelectAll" onclick="selectAllChk(this.checked);" type="checkbox" value=""
 6 name="chkID" runat="server">
 7    </HeaderTemplate>
 8    <ItemTemplate>
 9<INPUT id="chkID" onclick="AddRemoveValues(this);" type="checkbox" value='<%# DataBinder.Eval(Container.DataItem, "PI_ID") %>'  name="chkID" >
10    </ItemTemplate>
11</asp:TemplateColumn>

还可以参考孟宪会的跨页面实现多选