DamDamDeo a écrit :
Désolé de revenir à la charge, mais ce dresse devant moi un nouveau
problème.
En effet, ma page comporte de nombreux champs et donc de nombreux
calcul (uniquement des additions),
n'y a-t-il pas un moyen pour définir une fonction plus générale, dans
le sens on définirait les champs à additionner uniquement dans mes
divers champs total.
(je sais pas si je suis très clair)
ben, à ce moment là tu peux faire soit une fonction pas trop futée :
function add(prefix, nums) {
var f = document.form1;
nums = nums.split();
var tot = 0;
for(var i=0; i<nums.length; i++) tot += purge(f[prefix+num[i].value);
f[prefix+'total'].value = nbr2txt(tot);
}
<input tabindex=1 name="ch_acteur_18" type="text"
onchange="add('ch_acteur_', '12,15,18')" />
<input tabindex=2 name="ch_acteur_15" type="text"
onchange="add('ch_acteur_', '12,15,18')" />
<input tabindex=3 name="ch_acteur_12" type="text"
onchange="add('ch_acteur_', '12,15,18')" />
<input name="ch_acteur_total" />
<input tabindex=4 name="ch_chanteur_18" type="text"
onchange="add('ch_acteur_', '12,15,18')" />
<input tabindex=5 name="ch_chanteur_15" type="text"
onchange="add('ch_acteur_', '12,15,18')" />
<input tabindex=6 name="ch_chanteur_18" type="text"
onchange="add('ch_acteur_', '12,15,18')"/>
<input name="ch_chanteur_total" />
- si on n'a pas l'usage des id, autant s'en passer
- pour la taille des champs, hop! CSS --> input { width: 50px }
- type="text" est facultatif (c'est l'état par défaut des inputs)
Soit une fonction un peu + compliquée :
<html>
<script type="text/javascript">
function purge(nbre) {
nbre = nbre.replace(',','.');
nbre = nbre.replace(/([^0-9 ^.]|\.{2,5}|\s)/g,'');
return nbre*1; // on renvoie un nombre
}
function nbr2txt(n) {
return n.toString().replace('.',',');
}
function add(quoi) {
var t = quoi.parentNode;
while(t.tagName != 'TR') t = t.parentNode; // rangée contenant l'input
t = t.getElementsByTagName('INPUT'); // collection des inputs de ce rang
var L = t.length-1;
var n = 0;
for(var i=0; i<L; i++) n += purge(t[i].value); // addition des input
t[L].value = nbr2txt(n); // hop en nombre avec ','
}
</script>
<style type="text/css">
input { width: 5em; text-align: center }
input:focus { background: #ffc }
th input { border-color: red }
</style>
<table>
<tr>
<td><input tabindex="1" name="ch_act_18" onchange="add(this)" /></td>
<td><input tabindex="2" name="ch_act_15" onchange="add(this)" /></td>
<td><input tabindex="3" name="ch_act_12" onchange="add(this)" /></td>
<td><input tabindex="3" name="ch_act_28" onchange="add(this)" /></td>
<th><input name="ch_act_total" /></th>
</tr>
<tr>
<td><input tabindex="4" name="ch_chant_18" onchange="add(this)" /></td>
<td><input tabindex="5" name="ch_chant_15" onchange="add(this)" /></td>
<td><input tabindex="6" name="ch_chant_12" onchange="add(this)" /></td>
<td><input tabindex="6" name="ch_chant_28" onchange="add(this)" /></td>
<th><input name="ch_chant_total" /></th>
</tr>
</table>
</html>
ne reste qu'à additionner les rangées ... :-)
--
sm