1

这是问题所在:我正在尝试按组检查和取消选中复选框。

因此,如果您选择 G1 和 G2,它会抛出一条消息,您不能混合组。如果您取消选中所有这些,我正在尝试清除现有的分组,这就是代码似乎失败的地方。

有什么想法吗?(另外,我一开始可能对那个全局变量有一个错误的想法。所以请建议)

<HTML>
<script language="javascript">
var prodSel="";
function VerifyGroup(a,b)
    {
    ClearAllSelectionsA(); // check if this is the last unselect and clear the prodSel variable
        if (prodSel == "") 
            {
                prodSel = a;
            }else 
            {
            if (prodSel != a)
                {
                alert ( "Please ensure that the groups are same for the items you select");
                //alert(b);
                document.getElementById(b).checked  = false;
                }
            }

}

function ClearAllSelections()
{
    var inputs = document.getElementsByTagName("input"); 
    var cbs = []; 
        for (var i = 0; i < inputs.length; i++) 
            {  
                if (inputs[i].type == "checkbox") 
                    {  
                        inputs[i].checked = false;
                    }  
            }  
prodSel=""; // Clear the  variable; allow new selections
}

/*loop through and if all of them are unchecke,d clear the variable*/
function ClearAllSelectionsA()
{
var clre = true;
    var inputs = document.getElementsByTagName("input"); 
    var cbs = []; 
        for (var i = 0; i < inputs.length; i++) 
            {  
                if (inputs[i].type == "checkbox") 
                    {  
                        if (inputs[i].checked){clre= false;}
                    }  
            }  
            if (clre){
prodSel=""; // Clear the  variable; allow new selections
alert(window.prodSel);
}
}
</script>
<body>


G1<input type="checkbox" value="zxc" id="12" onclick="javascript:VerifyGroup('g1',12);"><BR>
G1<input type="checkbox" value="zxcw" id="123" onclick="javascript:VerifyGroup('g1',123);"><BR>
G1<input type="checkbox" value="zxcdw" id="124" onclick="javascript:VerifyGroup('g1',124);"><BR>
G2<input type="checkbox" value="zxcf" id="125" onclick="javascript:VerifyGroup('g2',125);"><BR>
G2<input type="checkbox" value="zxcfg" id="126" onclick="javascript:VerifyGroup('g2',126);"><BR>
<a href="#" onclick="ClearAllSelections();">clear group</a>
</body>
</html>
4

1 回答 1

1

当您调用 时ClearAllSelectionsA,如果未选中所有复选框,则将prodSel被清除。然后再回来VerifyGroupprodSel马上就被调职了a。我的建议是从该值返回 true 或 falseClearAllSelectionsA并根据该值采取行动。

<script language="javascript">
var prodSel="";
function VerifyGroup(a,b)
{
    var cleared = ClearAllSelectionsA(); // check if this is the last unselect and clear the prodSel variable
    if (prodSel == "" && !cleared) //only reset prodSel if there is a checkbox checked
    {
        prodSel = a;
    }else 
    {
        if (prodSel != a)
        {
            alert ( "Please ensure that the groups are same for the items you select");
            //alert(b);
            document.getElementById(b).checked  = false;
        }
    }
}

function ClearAllSelections()
{
    var inputs = document.getElementsByTagName("input"); 
    var cbs = []; 
    for (var i = 0; i < inputs.length; i++) 
    {
        if (inputs[i].type == "checkbox") 
        {
            inputs[i].checked = false;
        }
    }
    prodSel=""; // Clear the  variable; allow new selections
}

/*loop through and if all of them are unchecke,d clear the variable*/
function ClearAllSelectionsA()
{
    var clre = true;
    var inputs = document.getElementsByTagName("input"); 
    var cbs = []; 
        for (var i = 0; i < inputs.length; i++) 
        {  
            if (inputs[i].type == "checkbox") 
            {  
                if (inputs[i].checked){clre= false;}
            }  
        }  
        if (clre){
             prodSel=""; // Clear the  variable; allow new selections
             alert(window.prodSel);
        }
        return clre;
    }
</script>
于 2013-03-14T17:28:03.997 回答