Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Fala galera!Seguinte, tenho uma tabela exibida com vários CHECKBOX para seleção.Gostaria de saber como faço para marcar todos com apenas 1 click?Valeu
E com nomes diferentes?
oh mais ou menos assim
function marcaTodosNomesDiferentes(){ var obj = document.getElementById("form").children; var total = obj.length-1; for(i = 0;i<total;i++){ obj.item(i).checked = true; }} </script><html><body ><form name = "form" id = "form"><input type = "checkBox" name = "check" ><input type = "checkBox" name = "check1" ><input type = "checkBox" name = "check2" ><input type = "checkBox" name = "check3" ><input type = "checkBox" name = "check4" ><input type = "checkBox" name = "check5" ><input type = "button" name = "checa" value = "checa" onclick = marcaTodosNomesDiferentes(); ></form></body></html>um exemplo com checkbox e outro com select multiple:
nesse exemplo voce clica para selecionar e deselecionar
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Seleciona tudo</title>
<script>
function selecionar_todas(retorno) {
var frm = document.form1;
for(i = 0; i < frm.length; i++) {
if(frm.elements[i].type == "checkbox") {
frm.elements[i].checked = retorno;
}
}
}
function seleciona_tudo(retorno){
var s = document.form2.select;
for(var i = 0; i < s.options.length; i++) {
s.options[i].selected = retorno ? 'selected' : false;
}
}
</script>
</head>
<body>
<table width="594" height="204" border="1">
<tr>
<td width="274"><form id="form1" name="form1" method="post" action="">
<p>
<input type="checkbox" name="checkbox" value="1" onclick="return selecionar_todas(this.checked);" />
Seleciona Todas</p>
<p>
<input type="checkbox" name="checkbox2" value="1" />
<br />
<input type="checkbox" name="checkbox3" value="1" />
<br />
<input type="checkbox" name="checkbox4" value="1" />
<br />
<input type="checkbox" name="checkbox5" value="1" />
</p>
</form> </td>
<td width="304" valign="top"><form id="form2" name="form2" method="post" action="">
<p>
<input type="checkbox" name="checkbox6" value="checkbox" onclick="return seleciona_tudo(this.checked);" />
Seleciona Todas</p>
<p>
<select name="select" size="5" multiple="multiple">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br />
<br />
<br />
</p>
</form> </td>
</tr>
</table>
</body>
</html>Funcionou!Valeu galera.
Um ex pra você...
Ai no caso eles tem que ter o mesmo nome..beleza..Abraço!