我无法在以下代码中设置Global变量。我究竟做错了什么?

时间:2013-03-14 17:09:09

标签: javascript variables

问题在于:我正在尝试检查并取消选中一个组的复选框。

因此,如果您选择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>

1 个答案:

答案 0 :(得分:1)

当您致电ClearAllSelectionsA时,如果未选中所有复选框,则会清除prodSel。然后返回VerifyGroupprodSel会立即重新分配到a。我的建议是从ClearAllSelectionsA返回真或假,并根据该值进行操作。

<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>