Javascript来检查复选框是否被选中
我有一个javascript例程,它在一组复选框上执行操作,但是我想根据用户是选中还是取消选中,将单击的复选框设置为选中还是取消选中 不幸的是,每次我检查它是被选中还是未选中时,它都返回“on”,表示用户总是选中该框!任何帮助都将不胜感激,我还提供了javascriptJavascript来检查复选框是否被选中,javascript,Javascript,我有一个javascript例程,它在一组复选框上执行操作,但是我想根据用户是选中还是取消选中,将单击的复选框设置为选中还是取消选中 不幸的是,每次我检查它是被选中还是未选中时,它都返回“on”,表示用户总是选中该框!任何帮助都将不胜感激,我还提供了javascript // Uncheck all the checkboxs with the same Tax Credit for (i=0; i<arrChecks.length; i++) { var attribute =
// 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;
}
}
//取消选中具有相同税收抵免的所有复选框
对于(i=0;i而言,复选框的值
属性是您通过以下方式设置的:
<input type='checkbox' name='test' value='1'>
为此:
arrChecks[i].checked = !arrChecks[i].checked;
应该可以。你应该使用true
和false
而不是0
和1
来解决这个问题。我不确定问题出在哪里,但我很确定这会解决问题
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;
}
}
for(i=0;i您应该根据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;
}
}
for(i=0;i也要确保在firefox和IE中都进行了测试。JS操纵的复选框存在一些令人讨厌的错误。要切换复选框,或者可以使用
element.checked = !element.checked;
所以你可以用
if (attribute == elementName)
{
arrChecks[i].checked = !arrChecks[i].checked;
} else {
arrChecks[i].checked = false;
}
功能检查(){
var ChkBox=document.getElementById(“CheckBox1”);
警报(ChkBox.Checked);
}
在html中选中复选框的正确方法是checked=“checked”您考虑过像jquery、prototype、moo或yui这样的库吗?它们使这变得更容易了,因此代码将是:“if(arrChecks[i].checked)arrChecks[i].checked=1;”。那也不对。很公平。没有查看那里的逻辑。已修复。我正在处理其他项目,如果(arrChecks[I]。checked(-1)应该是ChkBox.checked,因为它区分大小写。在意识到错误之前,我花了大约20分钟的时间尝试让您的示例正常工作!
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');
}
}
function CHeck(){
var ChkBox = document.getElementById("CheckBox1");
alert(ChkBox.Checked);
}
<asp:CheckBox ID="CheckBox1" runat="server" onclick="CHeck()" />