如何在状态变化时获得状态并提升事件?

时间:2015-06-15 21:31:32

标签: javascript jquery

我在页面上有以下复选框用于课程。我想获取页面加载时检查的复选框的值,现在在提交页面时取消选中。我怎么能用JavaScript做到这一点?

</tr>
<tr valign="baseline" class="oddRow">
<td class="bold">0191</td>
<td class="bold">ART 1 (P)</td>
<td class="bold">S1</td>
<td>A</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td bgcolor="DarkGray" style="background-color:DarkGray">1<input type="checkbox" name="Periods" value="25325798" TITLE="0191.1 :  31/36&#xa;&nbsp;Thomas, Marc L" checked></td>
<td bgcolor="Indianred" style="background-color:Indianred">1*<input type="checkbox" name="Periods" value="25325799" TITLE="0191.2 :  36/36&#xa;&nbsp;Ericson, Daniel J"></td>
<td bgcolor="Indianred" style="background-color:Indianred">1*<input type="checkbox" name="Periods" value="25325800" TITLE="0191.3 :  36/36&#xa;&nbsp;Ericson, Daniel J"></td>
<td bgcolor="Indianred" style="background-color:Indianred">1*<input type="checkbox" name="Periods" value="25325801" TITLE="0191.4 :  37/36&#xa;&nbsp;Thomas, Marc L"></td>
</tr>

1 个答案:

答案 0 :(得分:1)

您可以使用window.onload,然后遍历复选框以查找已选中的复选框。

<script>
window.onload = function()
{
    var cbs = document.getElementsByClassName('checkbox');

    for(var i = 0; i < cbs.length; i++)
    {
        if(cbs[i].checked)
        {
            console.log(cbs[i].value);
        }
    }
}
</script>

</tr>
<tr valign="baseline" class="oddRow">
<td class="bold">0191</td>
<td class="bold">ART 1 (P)</td>
<td class="bold">S1</td>
<td>A</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td bgcolor="DarkGray" style="background-color:DarkGray">1<input type="checkbox" class="checkbox" name="Periods" value="25325798" TITLE="0191.1 :  31/36&#xa;&nbsp;Thomas, Marc L" checked></td>
<td bgcolor="Indianred" style="background-color:Indianred">1*<input type="checkbox" class="checkbox" name="Periods" value="25325799" TITLE="0191.2 :  36/36&#xa;&nbsp;Ericson, Daniel J"></td>
<td bgcolor="Indianred" style="background-color:Indianred">1*<input type="checkbox" class="checkbox" name="Periods" value="25325800" TITLE="0191.3 :  36/36&#xa;&nbsp;Ericson, Daniel J"></td>
<td bgcolor="Indianred" style="background-color:Indianred">1*<input type="checkbox" class="checkbox" name="Periods" value="25325801" TITLE="0191.4 :  37/36&#xa;&nbsp;Thomas, Marc L"></td>
</tr>