IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Formulaire dynamique devis


Sujet :

JavaScript

  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2004
    Messages : 12
    Par défaut Formulaire dynamique devis
    Bonjour,

    Ayant effectué des recherches, je ne suis pas parvenu à trouver une astuce pour faire fonctionner mon script sous IE.

    J'arrive à ajouter et à supprimer des lignes mais les calculs des totaux, calculs de tva ne fonctionnent pas avec ce navigateur.

    Par avance, merci

    Voici le code:
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
    <html xmlns="<a href="http://www.w3.org/1999/xhtml" target="_blank">http://www.w3.org/1999/xhtml</a>">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Document sans nom</title>
    </head>
    <style type="text/css">
    body { font-size: 11px; }
    table { border: 1px solid #009966; }
    th { font-size: 11px; font-weight:bold; padding: 3px 0px; vertial-align: top; }
    tbody td { text-align: center;  }
    input { border: 1px solid #999999; }
    .errInput { border: 1px solid #CC3300; }
    </style>
    <script type="text/javascript">
    var countLine = 1;
    removeLine = function( o ) {
     el = document.getElementById('devis-content');
     countLine = countLine - 1;
     rem = o.parentNode.parentNode;
     //alert(rem);
     el.removeChild( rem );
    }
    invalidEntry = function( o, state ) {
     var ncolor = '#FFFFFF';
     if(state) {
      ncolor = '#FF9999';
     }
     //alert(it);
     o.style.backgroundColor = ncolor;
    }
    sumPrice = function() {
     var qtys = document.getElementsByName('quantity[]');
     var bpws = document.getElementsByName('base_price_wtax[]');
     var pwts = document.getElementsByName('price_wtax[]');
     
     var ecus = document.getElementsByName('ecotax_unit[]');
     var ecos = document.getElementsByName('ecotax[]');
     
     for(i=0; i<countLine; i++) {
      //alert(document.getElementsByName('quantity[]').length);
      qtys[i].value = qtys[i].value.replace(/,/, '.');
      bpws[i].value = bpws[i].value.replace(/,/, '.');
      ecus[i].value = ecus[i].value.replace(/,/, '.');
      if(qtys.length && bpws.length && ecus.length) {
     
       var qty = parseFloat(qtys[i].value);
       var bpw = parseFloat(bpws[i].value);
       var eco = parseFloat(ecus[i].value);
       if(!isNaN(qty)) {
        if(!isNaN(bpw)) {
         invalidEntry(qtys[i], 0);
         invalidEntry(bpws[i], 0);
         qtys[i].value = qty;
         bpws[i].value = bpw;
         val = parseFloat(qty * bpw);
         if(isNaN(val)) {
          pwts[i].value = 'Err'; 
         }
         else {
          pwts[i].value = val;
         }
        }
        else {
         invalidEntry(bpws[i], 1);
        }
        if(!isNaN(eco)) {
         invalidEntry(ecus[i], 0);
         ecus[i].value = eco;
         val = parseFloat(qty * eco);
     
         if(isNaN(val)) {
          ecos[i].value = 'Err';
         }
         else {
          ecos[i].value = val;
         }
        }
       }
       else {
        invalidEntry(qtys[i], 1);
       }
      }
     }
     
     var sum_wt = document.getElementsByName('sum_price_wtax')[0];
     var sum_eco = document.getElementsByName('sum_ecotax')[0];
     
     var ts = 0;
     var tec = 0;
     
     for (i=0; i < countLine; i++) {
     
      ts = ts + parseFloat(pwts[i].value);
      tec = tec + parseFloat(ecos[i].value);
     }
     sum_wt.value = ts;
     sum_eco.value = tec;
     
     var tva = document.getElementsByName('tva_rate')[0];
     var sumtva = document.getElementsByName('sum_tva')[0];
     var ecotva = document.getElementsByName('eco_tva')[0];
     
     var totsum = document.getElementsByName('sum_total')[0];
     var toteco = document.getElementsByName('eco_total')[0];
     
     tva.value = tva.value.replace(/,/, '.');
     tva.value = parseFloat(tva.value);
     
     if(!isNaN(tva.value)) {
      invalidEntry(tva, 0);
      sumtva.value = (tva.value * sum_wt.value) / parseFloat(100);
      ecotva.value = (tva.value * sum_eco.value) / parseFloat(100);
     
      totsum.value = parseFloat(sum_wt.value) + parseFloat(sumtva.value);
      toteco.value = parseFloat(sum_eco.value) + parseFloat(ecotva.value);
     }
     else {
      invalidEntry(tva, 1);
     }
    }
    addLine = function() {
     el = document.getElementById('devis-content');
     countLine = countLine + 1;
     
     in_desc = document.createElement('input');
     in_desc.setAttribute('type', 'text');
     in_desc.setAttribute('name', 'label[]');
     in_desc.setAttribute('size', '65');
     in_desc.setAttribute('maxlength', '255');
     cl_desc = document.createElement('td');
     cl_desc.appendChild(in_desc);
     
     in_qty = document.createElement('input');
     in_qty.setAttribute('type', 'text');
     in_qty.setAttribute('name', 'quantity[]');
     in_qty.setAttribute('size', '5');
     in_qty.setAttribute('maxlength', '5');
     in_qty.onchange = function () { sumPrice(); }
     //in_qty.addEventListener('change', sumPrice, false);
     //in_qty.setAttribute('onChange', 'sumPrice();');
     cl_qty = document.createElement('td');
     cl_qty.appendChild(in_qty);
     
     in_basep = document.createElement('input');
     in_basep.setAttribute('type', 'text');
     in_basep.setAttribute('name', 'base_price_wtax[]');
     in_basep.setAttribute('size', '10');
     in_basep.setAttribute('maxlength', '10');
     in_basep.onchange = function () { sumPrice(); }
     //in_basep.addEventListener('change', sumPrice, false);
     //in_basep.setAttribute('onChange', 'sumPrice();');
     cl_basep = document.createElement('td');
     cl_basep.appendChild(in_basep);
     
     in_ecu = document.createElement('input');
     in_ecu.setAttribute('type', 'text');
     in_ecu.setAttribute('name', 'ecotax_unit[]');
     in_ecu.setAttribute('size', '8');
     in_ecu.setAttribute('maxlength', '8');
     in_ecu.onchange = function () { sumPrice(); }
     //in_ecu.setAttribute('onChange', 'sumPrice();');
     //in_ecu.addEventListener('change', sumPrice, false);
     cl_ecu = document.createElement('td');
     cl_ecu.appendChild(in_ecu);
     
     in_price = document.createElement('input');
     in_price.setAttribute('type', 'text');
     in_price.setAttribute('name', 'price_wtax[]');
     in_price.setAttribute('size', '12');
     in_price.setAttribute('maxlength', '12');
     in_price.setAttribute('readonly', 'readonly');
     cl_price = document.createElement('td');
     cl_price.appendChild(in_price);
     
     in_eco = document.createElement('input');
     in_eco.setAttribute('type', 'text');
     in_eco.setAttribute('name', 'ecotax[]');
     in_eco.setAttribute('size', '8');
     in_eco.setAttribute('maxlength', '8');
     cl_eco = document.createElement('td');
     cl_eco.appendChild(in_eco);
     
     in_btn = document.createElement('input');
     in_btn.setAttribute('type', 'button');
     in_btn.setAttribute('value', ' [-] ');
     in_btn.onclick = function () { removeLine(this); }
     //in_btn.setAttribute('onClick', 'removeLine(this);');
     //in_btn.setAttribute('click', 'removeLine(this
     cl_btn = document.createElement('td');
     cl_btn.appendChild(in_btn);
     
     ln = document.createElement('tr');
     ln.appendChild(cl_desc);
     ln.appendChild(cl_qty);
     ln.appendChild(cl_basep);
     ln.appendChild(cl_ecu);
     ln.appendChild(cl_price);
     ln.appendChild(cl_eco);
     ln.appendChild(cl_btn);
     
     el.appendChild(ln);
    }
    </script>
    <body>
    <div style="float: left;">
    <form id="form-devis" name="form-devis" method="post" action="">
      <table width="840" border="0" cellspacing="0" cellpadding="0">
      <thead>
        <tr>
          <td>&nbsp;</td>
          <td colspan="3">Date :</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="3">Devis n° :</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Client :</td>
          <td colspan="4">Pour :</td>
          <td>&nbsp;</td>
          </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="4">&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <th>Description</th>
          <th>QTY</th>
          <th>Prix HT Unitaire</th>
          <th>Ecotaxe Unitaire</th>
          <th width="74">Montant HT</th>
          <th width="60"><br/>
            dont EcoTaxe</th>
          <th><input type="button" value=" [+] " onclick="javascript:addLine();"/></th>
        </tr>
      </thead>
      <tbody id="devis-content">
        <tr>
          <td><input type="text" name="label[]" size="65" maxlength="255"/></td>
          <td><input type="text" name="quantity[]" size="5" maxlength="5" onChange="javascript:sumPrice();"/></td>
          <td><input type="text" name="base_price_wtax[]" size="10" maxlength="10" onChange="javascript:sumPrice();"/></td>
          <td><input type="text" name="ecotax_unit[]" size="8" maxlength="8" onChange="javascript:sumPrice();"/></td>
          <td><input type="text" name="price_wtax[]" size="12" maxlength="12" readonly="readonly" /></td>
          <td><input type="text" name="ecotax[]" size="8" maxlength="8" readonly="readonly"/></td>
          <td><input type="button" value=" [-] " onclick="javascript:removeLine(this);"/></td>
        </tr>
      </tbody>
      <tfoot>
       <tr>
         <td colspan="3">&nbsp;</td>
            <td>Sous-total HT</td>
            <td><input type="text" name="sum_price_wtax" size="12" maxlength="12" readonly="readonly"/></td>
            <td><input type="text" name="sum_ecotax" size="8" maxlength="8" readonly="readonly"/></td>
            <td>&nbsp;</td>
          </tr>
        <tr>
         <td colspan="3">&nbsp;</td>
            <td>Taux TVA</td>
            <td><input type="text" name="tva_rate" size="12" maxlength="6" onChange="javascript:sumPrice();"/></td>
            <td>%</td>
            <td>&nbsp;</td>
          </tr>
        <tr>
         <td colspan="3">&nbsp;</td>
            <td>TVA</td>
            <td><input type="text" name="sum_tva" size="12" maxlength="6" readonly="readonly"/></td>
            <td><input type="text" name="eco_tva" size="8" maxlength="8" readonly="readonly"/></td>
            <td>&nbsp;</td>
          </tr>
         <tr>
         <td colspan="3">&nbsp;</td>
            <td>Total</td>
            <td><input type="text" name="sum_total" size="12" maxlength="6" readonly="readonly"/></td>
            <td><input type="text" name="eco_total" size="8" maxlength="8" readonly="readonly"/></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
           <td colspan="5">&nbsp;</td>
            <td colspan="2"><input type="button" name="valid" value="Valider"/></td>
          </tr>
      </tfoot>
      </table>
    </form>
      <table width="840" border="0" cellpadding="0" cellspacing="0">
      <tbody>
     
      </tbody>
      </table>
    </div>
    <div style="float: left;">
     
    </div>
    </body>
    </html>

  2. #2
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    rem = o.parentNode.parentNode;
    L'erreur vient surement de là : sous FF, une div du style
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <div>
    Blablabla
    </div>
    contient 3 noeuds : le 1er retour chariot, le texte et le 2e retour chariot, ce qui explique probablement ton o.parentNode.parentNode.
    Mais pour IE, le retour chariot n'est pas un noeud, dans l'exemple, la div ne contient qu'un node : le texte,
    donc ton code ne va pas au même endroit sous IE et sous les autres navigateurs... D'où l'erreur
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

  3. #3
    Membre averti
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2004
    Messages : 12
    Par défaut
    removeLine utilise this en paramètre qui est l'objet bouton [-]. J'ai utilisé ca pour aller au parent( ici le td), puis au parent de ce td(ici le tr), pour supprimer la ligne complete (tr). C'est une utilisation impropre, désolé

    J'apprends encore

    L'erreur doit venir du parcours des objets quantity[] ajoutés dynamiquement, par exemple.

    Voici le lien :
    http://joomla13.dyndns.org/index.html

  4. #4
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    Effectivement :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var qtys = document.getElementsByName('quantity[]');
    est incorrect, il faut mettre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var qtys = document.getElementsByName('quantity');
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

  5. #5
    Membre averti
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2004
    Messages : 12
    Par défaut
    Ca ne vient pas de là...

    Merci

  6. #6
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    Tu l'as fait pour tous ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     var qtys = document.getElementsByName('quantity[]');
     var bpws = document.getElementsByName('base_price_wtax[]');
     var pwts = document.getElementsByName('price_wtax[]');
     
     var ecus = document.getElementsByName('ecotax_unit[]');
     var ecos = document.getElementsByName('ecotax[]');
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

  7. #7
    Membre averti
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2004
    Messages : 12
    Par défaut
    J'ai dû remettre les crochets, sans l'objet était toujours null

  8. #8
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    Sinon,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    qtys[i].value = qty;
         bpws[i].value = bpw;
         val = parseFloat(qty * bpw);
    est maladroit,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    qtys[i].value = qty;
         bpws[i].value = bpw;
         val = parseFloat(qty) *parseFloat( bpw);
    Est préférable : imaginons que qty='3.2texte' et bpw='1.0retexte',

    parseFloat(qty * bpw) vaut NaN
    parseFloat(qty) *parseFloat( bpw) vaut 3.2
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. formulaire dynamique pour un devis
    Par nicow43 dans le forum Général Conception Web
    Réponses: 2
    Dernier message: 27/03/2014, 10h58
  2. [JSF] Implémentation d'un formulaire dynamique
    Par Fleep dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 01/03/2005, 19h00
  3. pb access formulaire dynamique
    Par jibouze dans le forum IHM
    Réponses: 3
    Dernier message: 12/01/2005, 09h39
  4. formulaire dynamique
    Par shirya dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 19/10/2004, 16h13
  5. [struts]Formulaire dynamique
    Par GreenJay dans le forum Struts 1
    Réponses: 8
    Dernier message: 28/05/2004, 16h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo