Hi!!
I need some help with one list of checkbox in my form. I made an example to show for you.
My problem: When I click in checkbox, the <input name=”valor”/> should to receive “disabled”. When I click again, the input should to receive “enable”.
Code:
[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>Insert title here</title>
<script type=”text/javascript”>
function habilita(){
var valores = document.getElementsByName(“valor”);
var servicos = document.getElementsByName(“codServico”);
for(var i = 0; i < servicos.length; i++){
//alert(servicos[i].checked);
if (servicos[i].checked == false){
valores[i].disabled = true;
valor = valores[i].value;
valores[i].value = ‘0’;
subValor(valor);
}
if (servicos[i].checked){
//alert(“CheckBox marcada”);
//alert(“Input desabilitada => “+valores[i].disabled);
if(valores[i].disabled == true)
valores[i].disabled = false;
else valores[i].disabled = true;
//alert(“Input desablitada => “+valores[i].disabled);
valores[i].focus();
break;
}
}
}
//Função que subtrai o valor passado por parâmetro do valor total do contrato, quando uma checkbox é desabilitada no formulário
function subValor(val){
valorTotal = document.getElementById(“valorTotal”).value;
document.getElementById(“valorTotal”).value = parseFloat(valorTotal) – parseFloat(subValor);
}
//FUNÇÃO QUE ADICIONA O VALOR DOS INPUTS DE SERVICOS AO INPUT VALOR TOTAL
function addValor(campo){
var val;
val = campo.value;
alert(campo);
if(document.getElementById(“valorTotal”).value > 0){
alert(“teste”);
val2 = document.getElementById(“valorTotal”).value;
soma = parseFloat(val)+ parseFloat(val2);
document.getElementById(“valorTotal”).value = soma;
}else
document.getElementById(“valorTotal”).value = val;
}
</script>
</head>
<body onload=”habilita();”>
<table>
<tr>
<td>
<input type=”checkbox” name=”codServico” onchange=”habilita()”/>
</td>
<td>
<input type=”text” name=”valor” id=”valor” onblur=”addValor(this)” />
</td>
</tr>
<tr>
<td>
<input type=”checkbox” name=”codServico” onchange=”habilita()”/>
</td>
<td>
<input type=”text” name=”valor” id=”valor” onblur=”addValor(this)” />
</td>
</tr>
<tr>
<td>
<input type=”checkbox” name=”codServico” onchange=”habilita()”/>
</td>
<td>
<input type=”text” name=”valor” id=”valor” onblur=”addValor(this)” />
</td>
</tr>
<tr>
<td>Valor Total</td>
<td>
<input type=”text” name=”valorTotal” id=”valorTotal” />
</td>
</tr>
</table>
</body>
</html>