Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript的复选框列表项_Javascript_.net_Select_Checkboxlist_Selectall - Fatal编程技术网

使用JavaScript的复选框列表项

使用JavaScript的复选框列表项,javascript,.net,select,checkboxlist,selectall,Javascript,.net,Select,Checkboxlist,Selectall,我有一个清单,里面有几个项目和一个all选项。用户可以选择全部,我希望它可以勾选所有选项,如果他们取消勾选全部,它将取消勾选所有选项 我用下面的代码完成了这项工作 <script language="javascript" type="text/javascript"> function CheckBoxListSelect(cbControl) //, state) { var chkBoxList = document.getElementById

我有一个清单,里面有几个项目和一个all选项。用户可以选择全部,我希望它可以勾选所有选项,如果他们取消勾选全部,它将取消勾选所有选项

我用下面的代码完成了这项工作

<script language="javascript" type="text/javascript">
    function CheckBoxListSelect(cbControl) //, state)
    {
        var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");

        alert(chkBoxCount[0].checked);

        for(var i=0;i<chkBoxCount.length;i++)
        {   
            chkBoxCount[i].checked = chkBoxCount[0].checked //state;
        }

        return false; 
    }
</script>

cblAffiliation.Attributes.Add("onclick", "javascript: CheckBoxListSelect ('" & cblAffiliation.ClientID & "');")

功能复选框列表选择(cbControl)/,状态)
{
var chkBoxList=document.getElementById(cbControl);
var chkBoxCount=chkBoxList.getElementsByTagName(“输入”);
警报(chkBoxCount[0]。已选中);

对于(var i=0;i只需检查单击的框是否为“全部”选项。如果是,则继续并更改其余框。如果不是,则检查所有选项以查看它们是否都已选中,以便更新“全部”复选框


编辑 您可能希望使用onChange,而不是onClick,onClick可能会在给定复选框上的值更改之前被调用


代码未检查,请原谅语法问题

<script language="javascript" type="text/javascript">
    function CheckBoxListSelect(cbControl) //, state)
    {
        var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");
        var clicked = this;

        alert(chkBoxCount[0].checked ? 'All is Checked' : 'All is not Checked');
        alert(clicked == chkBoxCount[0] ? 'You Clicked All' : 'You Didn't click All');

        var AllChecked = true; // Check the all box if all the options are now checked

        for(var i = 1;i < chkBoxCount.length; i++)
        {   
            if(clicked == chkBoxCount[0]) { // Was the 'All' checkbox clicked?
                chkBoxCount[i].checked = chkBoxCount[0].checked; // Set if so
            }
            AllChecked &= chkBoxCount[i].checked; // AllChecked is anded with the current box
        }

        chkBoxCount[0].checked = AllChecked;

        return false; 
    }
</script>

cblAffiliation.Attributes.Add("onchange", "javascript: CheckBoxListSelect ('" & cblAffiliation.ClientID & "');")

功能复选框列表选择(cbControl)/,状态)
{
var chkBoxList=document.getElementById(cbControl);
var chkBoxCount=chkBoxList.getElementsByTagName(“输入”);
var=这个;
警报(chkBoxCount[0]。已选中?'All is checked':'All is not checked');
警报(clicked==chkBoxCount[0]?“您单击了全部”:“您没有单击全部”);
var AllChecked=true;//如果现在选中了所有选项,请选中“全部”框
对于(变量i=1;i
只需检查单击的框是否为“全部”选项。如果是,则继续并更改其余框。如果不是,则检查所有选项以查看它们是否全部选中,以便更新“全部”复选框


编辑 您可能希望使用onChange,而不是onClick,onClick可能会在给定复选框上的值更改之前被调用


代码未检查,请原谅语法问题

<script language="javascript" type="text/javascript">
    function CheckBoxListSelect(cbControl) //, state)
    {
        var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");
        var clicked = this;

        alert(chkBoxCount[0].checked ? 'All is Checked' : 'All is not Checked');
        alert(clicked == chkBoxCount[0] ? 'You Clicked All' : 'You Didn't click All');

        var AllChecked = true; // Check the all box if all the options are now checked

        for(var i = 1;i < chkBoxCount.length; i++)
        {   
            if(clicked == chkBoxCount[0]) { // Was the 'All' checkbox clicked?
                chkBoxCount[i].checked = chkBoxCount[0].checked; // Set if so
            }
            AllChecked &= chkBoxCount[i].checked; // AllChecked is anded with the current box
        }

        chkBoxCount[0].checked = AllChecked;

        return false; 
    }
</script>

cblAffiliation.Attributes.Add("onchange", "javascript: CheckBoxListSelect ('" & cblAffiliation.ClientID & "');")

功能复选框列表选择(cbControl)/,状态)
{
var chkBoxList=document.getElementById(cbControl);
var chkBoxCount=chkBoxList.getElementsByTagName(“输入”);
var=这个;
警报(chkBoxCount[0]。已选中?'All is checked':'All is not checked');
警报(clicked==chkBoxCount[0]?“您单击了全部”:“您没有单击全部”);
var AllChecked=true;//如果现在选中了所有选项,请选中“全部”框
对于(变量i=1;i
我想这段代码会对你有所帮助

<script type="text/javascript">
function check(checkbox) 
{
 var cbl = document.getElementById('<%=CheckBoxList2.ClientID %>').getElementsByTagName("input");
 for(i = 0; i < cbl.length;i++) cbl[i].checked = checkbox.checked;
}
</script>

 <asp:CheckBox ID="CheckBox1" runat="server" onclick="check(this)" />
 <asp:CheckBoxList ID="CheckBoxList2" runat="server">
    <asp:ListItem>C1</asp:ListItem>
    <asp:ListItem>C2</asp:ListItem>
  </asp:CheckBoxList>

功能检查(复选框)
{
var cbl=document.getElementById(“”).getElementsByTagName(“输入”);
对于(i=0;i
我想这段代码会对你有所帮助

<script type="text/javascript">
function check(checkbox) 
{
 var cbl = document.getElementById('<%=CheckBoxList2.ClientID %>').getElementsByTagName("input");
 for(i = 0; i < cbl.length;i++) cbl[i].checked = checkbox.checked;
}
</script>

 <asp:CheckBox ID="CheckBox1" runat="server" onclick="check(this)" />
 <asp:CheckBoxList ID="CheckBoxList2" runat="server">
    <asp:ListItem>C1</asp:ListItem>
    <asp:ListItem>C2</asp:ListItem>
  </asp:CheckBoxList>

功能检查(复选框)
{
var cbl=document.getElementById(“”).getElementsByTagName(“输入”);
对于(i=0;i

函数CheckAll(){
var intIndex=0;
var rowCount=document.getElementById('chkTest').getElementsByTagName(“输入”).length;
对于(intIndex=0;intIndex

函数CheckAll(){
var intIndex=0;
var rowCount=document.getElementById('chkTest').getElementsByTagName(“输入”).length;
对于(intIndex=0;intIndex