Javascript radio button et addition

Résolu/Fermé
thejoker - 20 oct. 2007 à 18:11
 thejoker - 20 oct. 2007 à 18:57
Bonjour,
je n'arrive pas à recuperer les valeurs et les additonner a partir de value de radio buttons..;

ca marche avec champ texte( en commentaire , bien sur j'enleve la partie "if (option[0].checked==true){a=200;}" dans le script)

mon debugger me dit option has no properties...
"
<html>
<head>
<title>Addition</title>
<style>
body{
font-size:10pt;
font-family:Arial;
}
</style>
<script type="text/javascript">
<!--
function Additionner(eltID_1, eltID_2, eltID_Resultat){
var option = document.getElementById(eltID_1);
var adhesion = document.getElementById(eltID_2);
var TOTAL = document.getElementById(eltID_Resultat);


if (option[0].checked==true){a=200;}
if (option[1].checked==true){a=100;}
if (option[2].checked==true){a=100;}

var a = parseInt(option.value);
var b = parseInt(adhesion.value);

if (isNaN(a)) { a = 0; }
if (isNaN(b)) { b = 0; }
TOTAL.value=a+b;
}

function recalculer(){
Additionner("option","adhesion","TOTAL");
}
// -->
</script>
</head>
<body>
Savez-vous faire une addition ? LOL
<form>
<!--
<input type="text" value="0" size="5" id="option" onKeyUp="recalculer();">+
<input type="text" value="0" size="5" id="adhesion" onKeyUp="recalculer();">=
<input type="text" value="0" size="5" id="TOTAL">
-->
<input type="radio" id="option[0]" name="option" value="1" onClick ="recalculer()">
<input type="radio" id="option[1]" name="option" value="2" onClick ="recalculer()">
<input type="radio" id="option[2]" name="option" value="3" onClick ="recalculer()">
<input type="radio" id="adhesion" name="adhesion" value="150" onClick ="recalculer()">
<input type="radio" id="adhesion" name="adhesion" value="30" onClick ="recalculer()">
<input type="text" id="TOTAL" name="TOTAL" size="13" value="0">

</form>
</html>"
A voir également:

1 réponse

j'ai trouvé la solution
utiliser champ caché

<head>
<title>Addition</title>
<style>
body{
font-size:10pt;
font-family:Arial;
}
</style>
<script type="text/javascript">
<!--
function Additionner(eltID_1, eltID_2, eltID_Resultat){
var cacher1 = document.getElementById(eltID_1);
var cacher2 = document.getElementById(eltID_2);
var TOTAL = document.getElementById(eltID_Resultat);

var a = parseInt(cacher1.value);
var b = parseInt(cacher2.value);
if (isNaN(a)) { a = 0; }
if (isNaN(b)) { b = 0; }

TOTAL.value=a+b;
}
function recalculer(){
Additionner("cacher1","cacher2","TOTAL");
}
// -->
</script>
</head>
<body>
<form name=form>

<input type="radio" id="option" name="option" value="1" onClick ="document.form.cacher1.value=200;recalculer();">
<input type="radio" id="option" name="option" value="2" onClick ="document.form.cacher1.value=100,recalculer();">
<input type="radio" id="option" name="option" value="3" onClick ="document.form.cacher1.value=100;recalculer();">
<input type=hidden id="cacher1" value="0">
<input type="radio" id="adhesion" name="adhesion" value="150" onClick ="document.form.cacher2.value=150;recalculer();";>
<input type="radio" id="adhesion" name="adhesion" value="30" onClick ="document.form.cacher2.value=30;recalculer();";>
<input type=hidden id="cacher2" value="0">
<input type="text" id="TOTAL" name="TOTAL" size="13" value="0">

</form>
</html>
1