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 :

nom de variable dynamique


Sujet :

JavaScript

  1. #1
    Membre du Club Avatar de gwena54
    Homme Profil pro
    Webdesigner
    Inscrit en
    Mai 2007
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2007
    Messages : 68
    Points : 57
    Points
    57
    Par défaut nom de variable dynamique
    Bonjour à tous,

    je souhaite déclarer dynamiquement des noms de variables afin de les réutiliser dans d'autres fonctions mais je bloque un peu, voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function test(){
     
    	var tab = new Array("nom","prenom","mail");
    	var size = tab.length;
     
    	for(i=0;i<=size;i++){
    		var window[tab[i]] = document.nomform.elements[tab[i]].value;
    		}
    	alert(prenom);
    	}
    tab contient les noms de tous mes champs
    size est le nombre d'éléments du tableau précédent
    la boucle sert à définir le nom des champs de cette manière:
    - var window[tab[i]] pour dire var nom, var prenom...
    - document.nomform.elements[tab[i]].value; pour récupérer la value des champs de mon formulaire

    le problème c'est que mon i n'est pas pris en compte et que ça fonctionne uniquement si je remplace le i par 0,1 ou 2, ce qui correspond au nombre d'éléments de mon array.

    Est ce que vous auriez une solution à mon problème afin que mes variables puissent se créer entièrement dynamiquement?

    Merci d'avance et bon week end!
    Gwen

  2. #2
    Expert éminent sénior

    Homme Profil pro
    Inscrit en
    Janvier 2007
    Messages
    13 474
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2007
    Messages : 13 474
    Points : 36 571
    Points
    36 571
    Par défaut
    Bonjour,
    le dernier élément d'un array est length - 1 A+

  3. #3
    Membre expérimenté
    Avatar de ryan
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2003
    Messages
    956
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2003
    Messages : 956
    Points : 1 316
    Points
    1 316
    Billets dans le blog
    1
    Par défaut
    Yop!

    Il me semble tout d'abord qu'il y a une erreur dans le contrôle de la boucle: i doit être strictement inférieur à size, et non pas inférieur ou égal, non?

    Edit: grillé

  4. #4
    Membre averti Avatar de marts
    Inscrit en
    Février 2008
    Messages
    233
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 233
    Points : 425
    Points
    425
    Par défaut
    Enlèves var devant window.

  5. #5
    Membre du Club Avatar de gwena54
    Homme Profil pro
    Webdesigner
    Inscrit en
    Mai 2007
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2007
    Messages : 68
    Points : 57
    Points
    57
    Par défaut
    Merci de vos réponse, j'ai un peu avancé par contre maintenant il me met: document.nomform is undefined, et je ne vois absolument pas pourquoi...
    à tout hasard voici mon code, rien de plus simple...
    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
    <!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=ISO-8859-1" />
    <title>Document sans nom</title>
    <script type="text/javascript">
    function test(){
     
    	tab = new Array("nom","prenom","mail");
    	size = tab.length;
     
    	for(i=0;i < size;i++){
    		window[tab[i]] = document.nomform.elements[tab[i]].value;
    		}
    	alert(nom);
    	alert(prenom);
    	alert(mail);
    	}
    	window.onload=test();
    </script>
    </head>
     
    <body>
    <form method="post" name="nomform" id="nomform">
    	<input type="text" name="nom" value="valu nom" />
    	<input type="text" name="prenom" value="valu prenom" />
    	<input type="text" name="mail" value="valu email" />
    </form>
    </body>
    </html>

  6. #6
    Membre expérimenté
    Avatar de ryan
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2003
    Messages
    956
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2003
    Messages : 956
    Points : 1 316
    Points
    1 316
    Billets dans le blog
    1
    Par défaut
    Yop!

    Et ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    document.forms.monform.elements[tab[i]].value;

  7. #7
    Membre du Club Avatar de gwena54
    Homme Profil pro
    Webdesigner
    Inscrit en
    Mai 2007
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2007
    Messages : 68
    Points : 57
    Points
    57
    Par défaut
    ryan, avec ton code, ça me fait une erreur encore plus bizarre: TypeError: Undefined value

  8. #8
    Membre expérimenté
    Avatar de ryan
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2003
    Messages
    956
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2003
    Messages : 956
    Points : 1 316
    Points
    1 316
    Billets dans le blog
    1
    Par défaut
    Yop!


    Ouuui, j'ai fait une faute de frappe. Le nom de ton formulaire est "nomform" et pas "monform"...

    Donc:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    document.forms.nomform.elements[tab[i]].value;

  9. #9
    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
    Points : 91 220
    Points
    91 220
    Billets dans le blog
    20
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function test(){
    	tab = new Array("nom","prenom","mail");
            var nom, prenom, mail;
    	for(i=0;i < tab.length;i++){
    		tab[i] = document.forms['nomform'].elements[tab[i]].value;
    		}
    	alert(nom);
    	alert(prenom);
    	alert(mail);
    	}
    	window.onload=test();

  10. #10
    Membre du Club Avatar de gwena54
    Homme Profil pro
    Webdesigner
    Inscrit en
    Mai 2007
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2007
    Messages : 68
    Points : 57
    Points
    57
    Par défaut
    si je remplace alert(nom) par alert(tab[0]) ça fonctionne bien, j'ai la value du champs nom
    mais sinon ça ne fonctionne pas

  11. #11
    Membre averti Avatar de marts
    Inscrit en
    Février 2008
    Messages
    233
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 233
    Points : 425
    Points
    425
    Par défaut
    sans parenthèses.

  12. #12
    Membre du Club Avatar de gwena54
    Homme Profil pro
    Webdesigner
    Inscrit en
    Mai 2007
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2007
    Messages : 68
    Points : 57
    Points
    57
    Par défaut
    le seul résultat que j'ai c'est undefined concernant les variables nom, prenom, email
    le tab[i] ne fonctionne pas comme il devrait on dirait...

  13. #13
    Membre expérimenté
    Avatar de ryan
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2003
    Messages
    956
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2003
    Messages : 956
    Points : 1 316
    Points
    1 316
    Billets dans le blog
    1
    Par défaut
    Yop!

    Tu devrais peut-être faire un eval:

    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
     
    <!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=ISO-8859-1" />
    <title>Document sans nom</title>
    <script type="text/javascript">
    function test(){
     
    	tab = new Array("nom","prenom","mail");
    	size = tab.length;
     
    	for(i=0;i < size;i++){
    		eval(tab[i] + " = document.nomform.elements[tab[i]].value;");
    		}
    	alert(nom);
    	alert(prenom);
    	alert(mail);
    	}
    </script>
    </head>
     
    <body>
    <form method="post" name="nomform" id="nomform">
    	<input type="text" name="nom" value="valu nom" />
    	<input type="text" name="prenom" value="valu prenom" />
    	<input type="text" name="mail" value="valu email" />
    	<input type="button" onclick="test()">
    </form>
    </body>
    </html>

  14. #14
    Membre du Club Avatar de gwena54
    Homme Profil pro
    Webdesigner
    Inscrit en
    Mai 2007
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Webdesigner
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Mai 2007
    Messages : 68
    Points : 57
    Points
    57
    Par défaut
    ça fonctionne avec l'eval super!
    j'avais essayé de l'intégrerr dans ma fonction mais de la mauvaise manière à savoir eval(tab[i]) = document...

    bon ben voilà une chose de résolue!
    merci à vous pour l'aide!

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

Discussions similaires

  1. [JSTL] nom de variable "dynamique"
    Par gmonta31 dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 04/05/2006, 13h31
  2. Nom de variable dynamique
    Par mavina dans le forum Linux
    Réponses: 7
    Dernier message: 26/04/2006, 20h48
  3. Réponses: 4
    Dernier message: 13/09/2005, 11h50
  4. Noms de Variables dynamiques
    Par horec dans le forum Langage
    Réponses: 7
    Dernier message: 22/07/2005, 16h47
  5. [SQL SERVER 2000] Noms de variables dynamiques
    Par cassoulet dans le forum MS SQL Server
    Réponses: 9
    Dernier message: 08/09/2004, 11h44

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