And Garfunkel voili voilou | 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.
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... mais peut etre avec le code si dessous ca ira mieux: Voila le contenu de mon fichier test4.php!
Code :
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>essai</title>
- </head>
- <body>
- <?
- $year_id='13';
- $seg_name='total';
- $label_id='3';
- $label_id1='4';
- $label_id2='6';
- $label_id3='7';
- ?>
- <script language="javascript" type="text/javascript">
- //javascript function to calculate and format the data in the different fields
- function calculate(seg_name,year_id,label_id)
- {
- //function to round the values
- function roundit(Num, Places) {
- if (Places > 0) {
- if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
- var Rounder = Math.pow(10, Places);
- return Math.round(Num * Rounder) / Rounder;
- }
- else return Num;
- }
- else return Math.round(Num);
- }
- //declaration of variables
- var db,ib,rc,total;
- //creation of a regular expression to check if the user only enter , or . or numbers
- regularexpr = /[a-z]/g;
- regularexpr2 = /[A-Z]/g;
- //try to make the variable name working for example: total_13_3 it shows the right result
- var seg_name = 'total';
- var label_id = '3';
- var year_id = '13';
- var textname = seg_name.concat("_" );
- textname = textname.concat(year_id);
- textname = textname.concat("_" );
- textname = textname.concat(label_id);
- document.write(textname);
- // LE PROBLEME EST ICI!!!! JE NE PEUX PAS FAIRE FONCTIONNER TEXTNAME DANS LE CHEMIN CI DESSOUS POURTANT TEXTNAME RENVOIT BIEN :total_13_3!
- //get the data from the field and format the data in the field
- if((document.frmcal.textname.value.length==0)||(document.frmcal.textname.value==null))
- {db = parseFloat(0);}else{
- db = document.frmcal.textname.value;
- db += ''
- var r = db.indexOf("." );
- var j = db.indexOf("," );
- if(j > 0){db = db.replace(",", "." );}
- var m = db.indexOf("'" );
- if(m > 0){db = db.replace("'", "." );}
- db = db.replace(regularexpr,"" );
- db = db.replace(regularexpr2,"" );
- db2 = db;
- db = parseFloat(db);
- var q = db2.indexOf("." );
- var db2l = db2.length;
- //the function indexOf start form 0 and length from 1 that's why we had to add another 1 to the variable!
- if(db2l==q+1) {db2 += '0'}
- db2 = parseFloat(db2);
- db2 = roundit(db2,1);
- db2 += ''
- if(j<0 && m <0 && r<0){db2 += '.0'}
- db2 = db2.replace(".", "'" );
- document.frmcal.total_13_3.value = db2;
- }
- //get the data from the field and format the data in the field
- if((document.frmcal.total_13_4.value.length==0)||(document.frmcal.total_13_4.value==null))
- {ib = parseFloat(0);}else{
- ib = document.frmcal.total_13_4.value;
- ib += ''
- r = ib.indexOf("." );
- j = ib.indexOf("," );
- if(j > 0){ib = ib.replace(",", "." );}
- m = ib.indexOf("'" );
- if(m > 0){ib = ib.replace("'", "." );}
- ib = ib.replace(regularexpr,"" );
- ib = ib.replace(regularexpr2,"" );
- ib2 = ib;
- ib = parseFloat(ib);
- q = ib2.indexOf("." );
- if(j<0 && m <0 && r<0){ib2 += '.0'}
- ib2l = ib2.length;
- if(ib2l==q+1) {ib2 += '0'}
- ib2 = parseFloat(ib2);
- ib2 = roundit(ib2,1);
- ib2 += ''
- ib2 = ib2.replace(".", "'" );
- document.frmcal.total_13_4.value = ib2;
- }
- //get the data from the field and format the data in the field
- if((document.frmcal.total_13_6.value.length==0)||(document.frmcal.total_13_6.value==null))
- {rc = parseFloat(0);}else{
- rc = document.frmcal.total_13_6.value;
- rc += ''
- r = rc.indexOf("." );
- j = rc.indexOf("," );
- if(j > 0){rc = rc.replace(",", "." );}
- m = rc.indexOf("'" );
- if(m > 0){rc = rc.replace("'", "." );}
- rc = rc.replace(regularexpr,"" );
- rc = rc.replace(regularexpr2,"" );
- rc2 = rc;
- rc = parseFloat(rc);
- q = rc2.indexOf("." );
- if(j<0 && m <0 && r<0){rc2 += '.0'}
- rc2l = rc2.length;
- if(rc2l==q+1) {rc2 += '0'}
- rc2 = parseFloat(rc2);
- rc2 = roundit(rc2,1);
- rc2 += ''
- rc2 = rc2.replace(".", "'" );
- document.frmcal.total_13_6.value = rc2;
- }
- //Calculation of the 3 values :)
- total = db + ib + rc;
- total=roundit(total,1);
- total2 = parseFloat(total);
- //convert to a string
- total += ''
- //manipulations of the string to have a good formatting
- var i = total.indexOf("." );
- if(i < 0)
- {total += '\'0'}else{
- var ncar = total.indexOf("0" );
- if(ncar < 0){
- var totall = total.length;
- if (totall==i+1){total += '0'}
- }
- total = total.replace(".", "'" );
- }
- document.frmcal.total_13_7.value = total;
- }
-
-
- </script>
- <form action="bla" name="frmcal">
- <table width="200" border="1" cellspacing="0" cellpadding="1">
- <tr>
- <th scope="col"> </th>
- <th scope="col">actual 2005</th>
- </tr>
- <tr>
- <td>direct biz</td>
- <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>
- </tr>
- <tr>
- <td>inter biz</td>
- <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>
- </tr>
- <tr>
- <td>risks/chances</td>
- <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>
- </tr>
- <tr>
- <td>total</td>
- <td><input type="text" name="<? echo $seg_name.'_'.$year_id.'_'.$label_id3;?>" readonly="readonly" size="3" maxlength="6" />
-
- </td>
- </tr>
- </table>
- </body>
- </html>
|
Si vous avez une idee, Merci beaucoup! Message édité par And Garfunkel le 18-12-2007 à 10:43:04
|