我有一堆按类别组织的复选框。我有一个复选框,每个类别切换相应的复选框组。出于某种原因,它不适用于IE;但是,我能够单独选择所有/清除所有链接以正常工作。
请参阅此处的代码: http://jsfiddle.net/mobilemelody/DyZF4/1/
有关更多上下文,这些框用于在地图API中打开和关闭谷歌融合图层。谢谢!
修改
抱歉,我最初把代码放在JSfiddle中,因为我觉得这对人们来说会更容易..
使用Javascript:
function toggleOn(source) {
var checkboxes = source;
for(var i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = true;
}
function toggleOff(source) {
var checkboxes = source;
for(var i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = false;
}
function toggle(toggleBox, source) {
var checkboxes = source;
for(var i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = toggleBox.checked;
}
HTML:
<div>
<table>
<tr><td colspan="5"><label><strong>Routes:</strong></label></td></tr>
<tr>
<td><input type="checkbox" checked="checked" name="route" id="toggle-local" onClick="toggle(this, document.getElementsByClassName('local'))">local</td>
<td><input type="checkbox" checked="checked" name="route" id="17" value="17" class="local"/><label>17</label></td>
<td><input type="checkbox" checked="checked" name="route" id="19" value="19" class="local"/><label>19</label></td>
<td><input type="checkbox" checked="checked" name="route" id="22" value="22" class="local"/><label>22</label></td>
</tr><tr>
<td></td>
<td><input type="checkbox" checked="checked" name="route" id="23" value="23" class="local"/><label>23</label></td>
<td><input type="checkbox" checked="checked" name="route" id="29" value="29" class="local"/><label>29</label></td>
<td><input type="checkbox" checked="checked" name="route" id="35" value="35" class="local"/><label>35</label></td>
</tr><tr>
<td></td>
<td><input type="checkbox" checked="checked" name="route" id="36" value="36" class="local"/><label>36</label></td>
<td><input type="checkbox" checked="checked" name="route" id="45" value="45" class="local"/><label>45</label></td>
<td><input type="checkbox" checked="checked" name="route" id="49" value="49" class="local"/><label>49</label></td>
</tr><tr>
<td></td>
<td><input type="checkbox" checked="checked" name="route" id="51" value="51" class="local"/><label>51</label></td>
<td><input type="checkbox" checked="checked" name="route" id="71" value="71" class="local"/><label>71</label></td>
</tr><tr>
<td><input type="checkbox" checked="checked" name="route" id="toggle-stage" onClick="toggle(this, document.getElementsByClassName('stage'))">stage</td>
<td><input type="checkbox" checked="checked" name="route" id="61" value="61" class="stage"/><label>61</label></td>
<td><input type="checkbox" checked="checked" name="route" id="68" value="68" class="stage"/><label>68</label></td>
</tr><tr>
<td><input type="checkbox" checked="checked" name="route" id="toggle-shuttle" onClick="toggle(this, document.getElementsByClassName('shuttle'))">shuttle</td>
<td><input type="checkbox" checked="checked" name="route" id="222" value="222" class="shuttle"/><label>222</label></td>
<td><input type="checkbox" checked="checked" name="route" id="233" value="233" class="shuttle"/><label>233</label></td>
<td><input type="checkbox" checked="checked" name="route" id="257" value="257" class="shuttle"/><label>257</label></td>
</tr><tr>
<td></td>
<td><input type="checkbox" checked="checked" name="route" id="259" value="259" class="shuttle"/><label>259</label></td>
</tr><tr>
<td><input type="checkbox" checked="checked" name="route" id="toggle-school" onClick="toggle(this, document.getElementsByClassName('school'))">school</td>
<td><input type="checkbox" checked="checked" name="route" id="113" value="113" class="school"/><label>113</label></td>
<td><input type="checkbox" checked="checked" name="route" id="117" value="117" class="school"/><label>117</label></td>
<td><input type="checkbox" checked="checked" name="route" id="125" value="125" class="school"/><label>125</label></td>
</tr><tr>
<td></td>
<td><input type="checkbox" checked="checked" name="route" id="126" value="126" class="school"/><label>126</label></td>
<td><input type="checkbox" checked="checked" name="route" id="127" value="127" class="school"/><label>127</label></td>
<td><input type="checkbox" checked="checked" name="route" id="139" value="139" class="school"/><label>139</label></td>
</tr></table>
</div>
<a id="select-all" onClick="toggleOn(document.getElementsByName('route'))" style="cursor: pointer;">select all</a> |
<a id="clear-all" onClick="toggleOff(document.getElementsByName('route'))" style="cursor: pointer;">clear all</a>