用于检查是否正在选中或取消选中复选框的Javascript [英] Javascript to check whether a checkbox is being checked or unchecked

查看:105
本文介绍了用于检查是否正在选中或取消选中复选框的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript例程正在对一组复选框执行操作,但最后一个操作我想根据用户是否选中该框或取消选中来设置单击复选框以选中或取消选中。

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.

不幸的是,每当我检查它是否被检查或未选中时,它都会返回on,表示用户总是在检查该框!任何帮助将不胜感激,我还包括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! Any help would be appreciated, I've also included the 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;
    }
} 


推荐答案

你应该针对checkbox元素的checked属性进行评估。

You should be evaluating against the checked property of the checkbox element.

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

这篇关于用于检查是否正在选中或取消选中复选框的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆