简体   繁体   English

用于检查是否正在选中或取消选中复选框的Javascript

[英]Javascript to check whether a checkbox is being checked or unchecked

I have a javascript routine that is performing actions on a group of checkboxes, but the final action I want to set the clicked checkbox to checked or unchecked based on if the user was checking the box or unchecking. 我有一个javascript例程,正在对一组复选框执行操作,但最后一个操作我想根据用户是在检查框还是取消选中来设置单击复选框以选中或取消选中。

Unfortunately, every time I check for whether it is being checked or unchecked, it returns "on" indicating the user is always checking the box! 不幸的是,每当我检查它是否被检查或未选中时,它都会返回“on”,表示用户总是在检查该框! Any help would be appreciated, I've also included the javascript. 任何帮助将不胜感激,我也包括了javascript。

// Uncheck all the checkboxs with the same Tax Credit
for (i=0; i<arrChecks.length; i++)
{
    var attribute = arrChecks[i].getAttribute("xid")
    if (attribute == elementName)
    {
        // if the current state is checked, unchecked and vice-versa
        if (arrChecks[i].value == "on")   // <-- This is always returning true, even if the box is being unchecked
        {
            arrChecks[i].checked = 1;
        } else {
            arrChecks[i].checked = 0;
        }

    } else {
        arrChecks[i].checked = 0;
    }
} 

You should be evaluating against the checked property of the checkbox element. 您应该针对checkbox元素的checked属性进行评估。

for (i=0; i<arrChecks.length; i++)
{
    var attribute = arrChecks[i].getAttribute("xid")
    if (attribute == elementName)
    {
        // if the current state is checked, unchecked and vice-versa
        if (arrChecks[i].checked)
        {
            arrChecks[i].checked = false;
        } else {
            arrChecks[i].checked = true;
        }

    } else {
        arrChecks[i].checked = false;
    }
}

The value attribute of a checkbox is what you set by: checkboxvalue属性是您设置的:

<input type='checkbox' name='test' value='1'>

So when someone checks that box, the server receives a variable named test with a value of 1 - what you want to check for is not the value of it (which will never change, whether it is checked or not) but the checked status of the checkbox. 因此,当有人检查该框时,服务器会收到一个名为test的变量,其value 1 - 您要检查的内容不是它的value (它将永远不会更改,无论是否已检查),但checked状态为复选框。

So, if you replace this code: 所以,如果你替换这个代码:

if (arrChecks[i].value == "on") 
{
    arrChecks[i].checked = 1;
} else {
    arrChecks[i].checked = 0;
}

With this: 有了这个:

arrChecks[i].checked = !arrChecks[i].checked;

It should work. 它应该工作。 You should use true and false instead of 0 and 1 for this. 你应该使用truefalse而不是01

To toggle a checkbox or you can use 要切换复选框,您可以使用

element.checked = !element.checked;

so you could use 所以你可以使用

if (attribute == elementName)
{
    arrChecks[i].checked = !arrChecks[i].checked;
} else {
    arrChecks[i].checked = false;
}
function enter_comment(super_id) {
    if (!(document.getElementById(super_id).checked)) {
        alert('selected checkbox is unchecked now')
    } else {
        alert('selected checkbox is checked now');
    }
}

<input type="checkbox" name="a" id="1" value="1" onclick="enter_comment(this.value)" />

<input type="checkbox" name="b" id="2" value="2" onclick="enter_comment(this.value)" />

function CHeck(){
    var ChkBox = document.getElementById("CheckBox1");
    alert(ChkBox.Checked);
}

<asp:CheckBox ID="CheckBox1" runat="server" onclick="CHeck()" />

I am not sure what the problem is, but I am pretty sure this will fix it. 我不确定问题是什么,但我很确定这会解决它。

for (i=0; i<arrChecks.length; i++)
    {
        var attribute = arrChecks[i].getAttribute("xid")
        if (attribute == elementName)
        {
            if (arrChecks[i].checked == 0)  
            {
                arrChecks[i].checked = 1;
            } else {
                arrChecks[i].checked = 0;
            }

        } else {
            arrChecks[i].checked = 0;
        }
    }

Also make sure you test it both in firefox and IE. 还要确保你在firefox和IE中测试它。 There are some nasty bugs with JS manipulated checkboxes. JS操纵复选框有一些令人讨厌的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM