/    Sign up×
Community /Pin to ProfileBookmark

Some Help with list of checkbox

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>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 14.2008 — That is exactly what your code does. So that... where is your problem?
Copy linkTweet thisAlerts:
@bisildinhoauthorMay 14.2008 — Thanks Kor, but I achieve to resolve this problem. ?
×

Success!

Help @bisildinho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...