Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1644 connectés 

  FORUM HardWare.fr
  Programmation
  HTML/CSS

  [RESOLU] Integration variable dans un chemin javascript

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[RESOLU] Integration variable dans un chemin javascript

n°1659355
And Garfun​kel
voili voilou
Posté le 17-12-2007 à 14:03:46  profilanswer
 

Bonjour a tous,
 
J'aimerais savoir comment integrer une variable dans un chemin en javascript...  
 
voila j'ai une forme tres simple et je voudrais ajouter les valeurs des 3 premiers textfields et de montrer le resultat dans le dernier.
le probleme est que je voudrais pas faire ca seulement dans ce fichier de test la mais aussi a plus grande echelle dans un autre fichier.  :D  
C'est pourquoi j'ai besoin d'un chemin dynamique dans lequel year_id change et label_id aussi etc.  
 
je sais pas si c'est trop comprehensible... :D  
mais peut etre avec le code si dessous ca ira mieux:  :whistle:  
Voila le contenu de mon fichier test4.php!
 

Code :
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>essai</title>
  6. </head>
  7. <body>
  8. <?
  9. $year_id='13';
  10. $seg_name='total';
  11. $label_id='3';
  12. $label_id1='4';
  13. $label_id2='6';
  14. $label_id3='7';
  15. ?>
  16. <script language="javascript" type="text/javascript">
  17. //javascript function to calculate and format the data in the different fields
  18. function calculate(seg_name,year_id,label_id)
  19. {
  20.  //function to round the values
  21.   function roundit(Num, Places) {
  22.    if (Places > 0) {
  23.    if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
  24.    var Rounder = Math.pow(10, Places);
  25.    return Math.round(Num * Rounder) / Rounder;
  26.    }
  27.    else return Num;
  28.    }
  29.    else return Math.round(Num);
  30. }
  31.  //declaration of variables
  32. var db,ib,rc,total;
  33. //creation of a regular expression to check if the user only enter , or . or numbers
  34. regularexpr = /[a-z]/g;
  35. regularexpr2 = /[A-Z]/g;
  36.  //try to make the variable name working for example: total_13_3 it shows the right result
  37. var seg_name = 'total';
  38. var label_id = '3';
  39. var year_id = '13';
  40. var textname = seg_name.concat("_" );
  41. textname = textname.concat(year_id);
  42. textname = textname.concat("_" );
  43. textname = textname.concat(label_id);
  44. document.write(textname);
  45.  // LE PROBLEME EST ICI!!!! JE NE PEUX PAS FAIRE FONCTIONNER TEXTNAME DANS LE CHEMIN CI DESSOUS POURTANT TEXTNAME RENVOIT BIEN :total_13_3!  
  46.  //get the data from the field and format the data in the field
  47. if((document.frmcal.textname.value.length==0)||(document.frmcal.textname.value==null))
  48. {db = parseFloat(0);}else{
  49.  db = document.frmcal.textname.value;
  50.  db += ''
  51.  var r = db.indexOf("." );
  52.  var j = db.indexOf("," );
  53.  if(j > 0){db = db.replace(",", "." );}
  54.  var m = db.indexOf("'" );
  55.  if(m > 0){db = db.replace("'", "." );}
  56.  db = db.replace(regularexpr,"" );
  57.  db = db.replace(regularexpr2,"" );
  58.  db2 = db;
  59.  db = parseFloat(db);
  60.  var q = db2.indexOf("." );
  61.  var db2l = db2.length;
  62.  //the function indexOf start form 0 and length from 1 that's why we had to add another 1 to the variable!
  63.  if(db2l==q+1) {db2 += '0'}
  64.  db2 = parseFloat(db2);
  65.  db2 = roundit(db2,1);
  66.  db2 += ''
  67.  if(j<0 && m <0 && r<0){db2 += '.0'}
  68.  db2 = db2.replace(".", "'" );
  69.  document.frmcal.total_13_3.value = db2;
  70. }
  71.  //get the data from the field and format the data in the field
  72. if((document.frmcal.total_13_4.value.length==0)||(document.frmcal.total_13_4.value==null))
  73. {ib = parseFloat(0);}else{
  74.  ib = document.frmcal.total_13_4.value;
  75.  ib += ''
  76.  r = ib.indexOf("." );
  77.  j = ib.indexOf("," );
  78.  if(j > 0){ib = ib.replace(",", "." );}
  79.  m = ib.indexOf("'" );
  80.  if(m > 0){ib = ib.replace("'", "." );}
  81.  ib = ib.replace(regularexpr,"" );
  82.  ib = ib.replace(regularexpr2,"" );
  83.  ib2 = ib;
  84.  ib = parseFloat(ib);
  85.  q = ib2.indexOf("." );
  86.  if(j<0 && m <0 && r<0){ib2 += '.0'}
  87.  ib2l = ib2.length;
  88.  if(ib2l==q+1) {ib2 += '0'}
  89.  ib2 = parseFloat(ib2);
  90.  ib2 = roundit(ib2,1);
  91.  ib2 += ''
  92.  ib2 = ib2.replace(".", "'" );
  93.  document.frmcal.total_13_4.value = ib2;
  94. }
  95.  //get the data from the field and format the data in the field
  96. if((document.frmcal.total_13_6.value.length==0)||(document.frmcal.total_13_6.value==null))
  97. {rc = parseFloat(0);}else{
  98.  rc = document.frmcal.total_13_6.value;
  99.  rc += ''
  100.  r = rc.indexOf("." );
  101.  j = rc.indexOf("," );
  102.  if(j > 0){rc = rc.replace(",", "." );}
  103.  m = rc.indexOf("'" );
  104.  if(m > 0){rc = rc.replace("'", "." );}
  105.  rc = rc.replace(regularexpr,"" );
  106.  rc = rc.replace(regularexpr2,"" );
  107.  rc2 = rc;
  108.  rc = parseFloat(rc);
  109.  q = rc2.indexOf("." );
  110.  if(j<0 && m <0 && r<0){rc2 += '.0'}
  111.  rc2l = rc2.length;
  112.  if(rc2l==q+1) {rc2 += '0'}
  113.  rc2 = parseFloat(rc2);
  114.  rc2 = roundit(rc2,1);
  115.  rc2 += ''
  116.  rc2 = rc2.replace(".", "'" );
  117.  document.frmcal.total_13_6.value = rc2;
  118. }
  119. //Calculation of the 3 values :)
  120. total = db + ib + rc;
  121. total=roundit(total,1);
  122. total2 = parseFloat(total);
  123. //convert to a string
  124. total += ''
  125. //manipulations of the string to have a good formatting
  126. var i = total.indexOf("." );
  127. if(i < 0)
  128. {total += '\'0'}else{
  129.  var ncar = total.indexOf("0" );
  130.  if(ncar < 0){
  131.   var totall = total.length;
  132.   if (totall==i+1){total += '0'}
  133.  }
  134.  total = total.replace(".", "'" );
  135. }
  136. document.frmcal.total_13_7.value = total;
  137.   }
  138.  
  139.  
  140. </script>
  141. <form action="bla" name="frmcal">
  142. <table width="200" border="1" cellspacing="0" cellpadding="1">
  143.   <tr>
  144.     <th scope="col">&nbsp;</th>
  145.     <th scope="col">actual 2005</th>
  146.   </tr>
  147.   <tr>
  148.     <td>direct biz</td>
  149.     <td><input type="text" name="<? echo $seg_name.'_'.$year_id.'_'.$label_id;?>" size="3"  onchange="calculate(<? echo '\''.$seg_name.'\','.$year_id.','.$label_id;?> )" maxlength="6"/></td>
  150.   </tr>
  151.   <tr>
  152.     <td>inter biz</td>
  153.     <td><input type="text" name="<? echo $seg_name.'_'.$year_id.'_'.$label_id1;?>" size="3" onchange="calculate(<? echo '\''.$seg_name.'\','.$year_id.','.$label_id1;?> )" maxlength="6"/></td>
  154.   </tr>
  155.   <tr>
  156.     <td>risks/chances</td>
  157.     <td><input type="text" name="<? echo $seg_name.'_'.$year_id.'_'.$label_id2;?>" size="3" onchange="calculate(<? echo '\''.$seg_name.'\','.$year_id.','.$label_id2;?> )" maxlength="6"/></td>
  158.   </tr>
  159.   <tr>
  160.     <td>total</td>
  161.     <td><input type="text" name="<? echo $seg_name.'_'.$year_id.'_'.$label_id3;?>"   readonly="readonly" size="3" maxlength="6" />
  162.    
  163.     </td>
  164.   </tr>
  165. </table>
  166. </body>
  167. </html>


Si vous avez une idee, Merci beaucoup!  [:and garfunkel]


Message édité par And Garfunkel le 18-12-2007 à 10:43:04
mood
Publicité
Posté le 17-12-2007 à 14:03:46  profilanswer
 

n°1659361
kao98
...
Posté le 17-12-2007 à 14:12:11  profilanswer
 

Rien compris ! :/
Sois plus clair ! Et surtout, isole ton problème plutôt que de nous poster toute une page de code !

Message cité 1 fois
Message édité par kao98 le 17-12-2007 à 14:12:33

---------------
Kao ..98 - Uplay (R6S) : kao98.7.62x39 - Origin (BF4, BF1) : kntkao98
n°1659367
And Garfun​kel
voili voilou
Posté le 17-12-2007 à 14:29:01  profilanswer
 

kao98 a écrit :

Rien compris ! :/
Sois plus clair ! Et surtout, isole ton problème plutôt que de nous poster toute une page de code !


 
ok pas de soucis!
 
J'ai une fonction javascript qui calcule les valeurs des textfields, elle a 3 parametres: seg_name,year_id,label_id

Code :
  1. function calculate(seg_name,year_id,label_id)


 
dans la fonction je veux modifier les parametres de telle sorte qu'ils s'affiche seg_name _  year_id _ label_id (sans espaces) -> par example: total_13_3
 
C'est ce que je pense j'ai fait ci dessous:
 

Code :
  1. var textname = seg_name.concat("_" );
  2. textname = textname.concat(year_id);
  3. textname = textname.concat("_" );
  4. textname = textname.concat(label_id);


 
Mon but final est d'arriver a integrer la valeur de textname dans le chemin ci dessous par example mais cela ne fonctionne pas:  
 

Code :
  1. db = document.frmcal.textname.value;


 dans ce code textname devrait etre egal a 'total_13_3' par example mais non quand j'execute le code il reste a 'textname'!
 
As tu compris, kao?  :whistle:  
 
Je pense en effet que le premier poste etait peu clair, desole...  :ange:  

n°1659647
And Garfun​kel
voili voilou
Posté le 17-12-2007 à 23:50:31  profilanswer
 

une idee? ce n'est pas assez clair? :O

n°1659692
astryad
Posté le 18-12-2007 à 09:21:13  profilanswer
 

Tu as essayé en donnant des ids à tes différents input et en accédant à leur valeur avec :
 

Code :
  1. document.getElementById('...').value


 
?

n°1659735
And Garfun​kel
voili voilou
Posté le 18-12-2007 à 10:35:27  profilanswer
 

a oui tiens j'avais essaye pour un autre script mais pas pour celui ci! Merci bonne idee...

n°1659740
And Garfun​kel
voili voilou
Posté le 18-12-2007 à 10:42:24  profilanswer
 

astryad a écrit :

Tu as essayé en donnant des ids à tes différents input et en accédant à leur valeur avec :
 

Code :
  1. document.getElementById('...').value


 
?


 
c'est bon!  :D  ca marche!  [:and garfunkel]  merci astryad!  :jap:  
J'aurai du y penser hier...


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  HTML/CSS

  [RESOLU] Integration variable dans un chemin javascript

 

Sujets relatifs
intégration de plusieurs swf dans une page html[RESOLU]Afficher le poids des fichiers listés automatiquement en PHP
[c++][resolu]Problème de compilation.[C++] [résolu] Besoin d'un coup de main pour déboguer mon algo de tri
[Résolu] Site internet en japonais[Delphi] Problème dans un code / N'affiche jamais la variable
[resolu]Boucles sur des requetes MySQL[php] variable contenant un espace
[résolu] Automatiser une fonction excel sur vbaProbleme de variable VB Access
Plus de sujets relatifs à : [RESOLU] Integration variable dans un chemin javascript


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR