请问怎么实现两个checkbox都做全选按钮:
<input id="check" onclick="checkAll()" type="checkbox" name="checkbox1" value="checkbox"/>
<input id="check" onclick="checkAll()" type="checkbox" name="checkbox2" value="checkbox"/>
<script>
function checkAll() {
var checked = document.getElementById("check").checked;
var checkson = document.getElementsByName("checkbox2");
if(checked) {
for(var i = 0; i < checkson.length ;i++) {
checkson[i].checked = true;}}
else {
for(var i = 0; i < checkson.length ;i++) {
checkson[i].checked = false;}}
}
</script>
你想干嘛啊,没看明白。另外你的id也重复了
想这样实现,两个都可以全选
<input id="check1" onclick="javascript:$('#check2').click()" type="checkbox" name="checkbox1" value="checkbox"/>
<input id="check2" onclick="checkAll()" type="checkbox" name="checkbox2" value="checkbox"/>
多谢哦。还有位老哥这样写,分享一下:
<input id="check1" onclick="checkAll(this.checked)" type="checkbox" name="checkbox1" value="checkbox"/>
<input id="check2" onclick="checkAll(this.checked)" type="checkbox" name="checkbox1" value="checkbox"/>
<hr>
<input type="checkbox" name="checkbox2" value="checkbox"/>
<input type="checkbox" name="checkbox2" value="checkbox"/>
<input type="checkbox" name="checkbox2" value="checkbox"/>
<script>
function checkAll(checked) {
document.getElementById("check1").checked = checked;
document.getElementById("check2").checked = checked;
var checkson = document.getElementsByName("checkbox2");
if(checked) {
for(var i = 0; i < checkson.length ;i++) {
checkson[i].checked = true;}}
else {
for(var i = 0; i < checkson.length ;i++) {
checkson[i].checked = false;}}
}
</script>