Bonjour à tous,
J'ai un probléme bizarre, en gros j'ai créer une fonction en javascript qui doit me donner le total de la somme de 30 valeurs. Jusque la pas de souci, j'intégre ma fonction pour qu'elle marche et la c'est le drame.
Voici ma fonction :
Je m'explique :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 // PERMET DE CALCULER LE PRIX TOTAL function prixTotal() { var tot = 0 ; // TOTAL DES 30 LIGNES D'ELEMENTS for (k = 0 ; k < 30 ; k++) { if (document.getElementById('id_TOT'+k).value != "") { tot = Math.round((parseFloat(tot) + parseFloat(document.getElementById('id_TOT'+k).value)) * 100) / 100; } } document.getElementById('id_TOTAL').value = parseFloat(tot); }
Lorsque j'execute cette fonction dans une autre fonction mon element document.getElementById('id_TOTAL').value ne recupére pas la valeur 'tot' correspondant au total des lignes.
Par contre le fait d'ajouter un simple alert('test'); en premiere ligne de ma fonction ceci retourne bien la valeur.
J'ai aussi placé le alert('test'); dans la fonction qui utilise celle ci juste avant et la pas de probleme ca marche aussi mais des que je vire le alert('test'); ca marche plus...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 // PERMET DE CALCULER LE PRIX TOTAL function prixTotal() { alert('test'); var tot = 0 ; // TOTAL DES 30 LIGNES D'ELEMENTS for (k = 0 ; k < 30 ; k++) { if (document.getElementById('id_TOT'+k).value != "") { tot = Math.round((parseFloat(tot) + parseFloat(document.getElementById('id_TOT'+k).value)) * 100) / 100; } } document.getElementById('id_TOTAL').value = parseFloat(tot); }
...je sais pas pkoi j'ai pensé au temps d'execution de la boucle qui serait trop important mais bon l'addition de 30 nombres je pense pas que cela prenne tant de temps, et puis je pense que javascript n'execute la pochaine instruction que lorsque la precedente est terminée.
Donc si kk a une idée.
Fonction qui utilise la fonction prixTotal() qui marche pas :
Fonction qui utilise la fonction prixTotal() + alert() qui marche:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 function sendData(data, page, method, ligne, numero) { if ((data.substring(8, data.length) == "") || (((data.length - 8) == 8) && (isNaN(data.substring(8, data.length)) != true))) { // RECUPERER LE NAVIGATEUR UTILISE PAR L'UTILISATEUR ET CREE L'OBJET XMLHTTPREQUEST EN CONSEQUENCE if (document.all) { // OBJET XMLHTTPREQUEST POUR INTERNET EXPLORER var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ; } else { // OBJET XMLHTTPREQUEST POUR MOZILLA ET AUTRES NAVIGATEURS var XhrObj = new XMLHttpRequest(); } // TEST LE CHANGEMENT D'ETAT DE L'OBJET XhrObj.onreadystatechange = function() { // readyState == 4 : TOUTES LES DONNEES SONT CHARGEES // status == 200 : CODE REPONSE DU SERVEUR - 200 SIGNIFIE QUE LE SERVEUR EST OK if (XhrObj.readyState == 4 && XhrObj.status == 200) { document.getElementById(ligne).innerHTML = XhrObj.responseText; } } // OUVERTURE DU FICHIER PHP QUI CONTIENT LA REQUETE PAR LA METHODE POST XhrObj.open("POST", page); XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); XhrObj.send(data+numero); } else { alert('ATTENTION ! LE CODE SAP SAISI \"'+data.substring(8, data.length)+'\" EST INCORRECT.'); razValeur(numero); } // CALCUL LE PRIX TOTAL document.getElementById('id_TOTAL').value = ''; prixTotal(); }
J'avoue je coince un peu la autant pour xmlHTTPRequest pas de souci grace au tuto, idem pour les arrondis malgres queques heures de casse tete mais la je sais vraiment pas de quoi ca vient :s :$
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 function sendData(data, page, method, ligne, numero) { if ((data.substring(8, data.length) == "") || (((data.length - 8) == 8) && (isNaN(data.substring(8, data.length)) != true))) { // RECUPERER LE NAVIGATEUR UTILISE PAR L'UTILISATEUR ET CREE L'OBJET XMLHTTPREQUEST EN CONSEQUENCE if (document.all) { // OBJET XMLHTTPREQUEST POUR INTERNET EXPLORER var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ; } else { // OBJET XMLHTTPREQUEST POUR MOZILLA ET AUTRES NAVIGATEURS var XhrObj = new XMLHttpRequest(); } // TEST LE CHANGEMENT D'ETAT DE L'OBJET XhrObj.onreadystatechange = function() { // readyState == 4 : TOUTES LES DONNEES SONT CHARGEES // status == 200 : CODE REPONSE DU SERVEUR - 200 SIGNIFIE QUE LE SERVEUR EST OK if (XhrObj.readyState == 4 && XhrObj.status == 200) { document.getElementById(ligne).innerHTML = XhrObj.responseText; } } // OUVERTURE DU FICHIER PHP QUI CONTIENT LA REQUETE PAR LA METHODE POST XhrObj.open("POST", page); XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); XhrObj.send(data+numero); } else { alert('ATTENTION ! LE CODE SAP SAISI \"'+data.substring(8, data.length)+'\" EST INCORRECT.'); razValeur(numero); } // CALCUL LE PRIX TOTAL document.getElementById('id_TOTAL').value = ''; alert('test'); prixTotal(); }
Merci d'avance pour vos réponses.
Je vous tiens au courant si je trouve d'ici la.
Partager