我有一个 javascript 例程,它对一组复选框执行操作,但我想根据用户是否选中该框或取消选中将单击的复选框设置为选中或取消选中。
不幸的是,每次我检查它是否被选中或未选中时,它都会返回“on”,表明用户始终选中该框!如有任何帮助,我们将不胜感激,我还包含了 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;
}
}
最佳答案
您应该根据复选框元素的选中属性进行评估。
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 检查复选框是否被选中或取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/473562/