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 :

Test si bouton radio checked


Sujet :

JavaScript

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2012
    Messages : 91
    Points : 62
    Points
    62
    Par défaut Test si bouton radio checked
    Salut à tous,

    Alors voilà mon problème.
    J'essaye de faire une fonction en javascript qui me permet de vérifier si un bouton radio est checked mais quand je test, rien ne se passe..

    Voila ma fonction :

    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
     
    function formAbonnement()
    {
    	var xhr = null;
       	if(window.XMLHttpRequest)
       	{ // Firefox et autres
         	xhr = new XMLHttpRequest();
       	}
       	else
       	{
    	    if(window.ActiveXObject) 
    		{// Internet Explorer
         		try
    			{
    	   			xhr = new ActiveXObject("Msxml2.XMLHTTP");
    	 		}
    			catch (e)
    			{
    	   			xhr = new ActiveXObject("Microsoft.XMLHTTP");
    			}
        	} 
    		else 
    		{// XMLHttpRequest non supporté par le navigateur
    			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
    			xhr = false;
        	}
       	}
    	var requete = "";
    	requete += "proposition="+document.getElementsByName('proposition').value+"&choix="+document.getElementByName('choix').value;
    	xhr.open( 'POST', 'modele/abonnement.php',  true);
    	if(document.getElementByName('choix').checked == true)
    	{
    		document.getElementById("FormAbo").submit();
    	}
    	else
    	{
    		alert("pas bon");
    		return false;	
    	}
    	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xhr.send(requete);
    }
    et voila mon formulaire :
    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
     
    <form name="FormAbo" action="index.php?page=abonnement&action=souscrire" method="POST" id="FormAbo" onsubmit="formAbonnement();return false;">
    		<table border>				
    			<tr>
    				<td></td>
    				<td>Abonnement 1 mois<input type="radio"name="proposition" value="1" /></td>
    				<td> 25.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 3 mois<input type="radio"name="proposition" value="2" /></td>
    				<td> 70.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 6 mois<input type="radio"name="proposition" value="3" /></td>
    				<td> 150.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 9 mois<input type="radio"name="proposition" value="4" /></td>
    				<td> 250.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 12 mois<input type="radio"name="proposition" value="5" /></td>
    				<td> 350.00 Euros</td>
    			</tr>             
    		</table>
     
      	Moyen de paiement : </br>
      	<input type="radio" name="choix" value="cartebleue"><img src="images/CB.jpg" height="128px" width="128px" ALIGN="middle"></br>
    	<input type="radio" name="choix" value="paypal"><img src="images/paypal.jpg" height="128px" width="128px" ALIGN="middle"></br>
    	<input type="radio" name="choix" value="cheque"><img src="images/cheque.png" height="128px" width="128px" ALIGN="middle"></br>
    	</br>
    	<input type="button" value="Valider" onclick="formAbonnement()" name="FormAbo""/>
    	</form>
    Voila, merci de m'éclairer ^^

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    document.getElementByName('choix').checked


    C'est nouveau getElementByName ?

    C'est getElementsByName

    qui retourne une collection d'objets

    il faut donc boucler sur la collection et teste l'indice
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    document.getElementsByName('choix')[i].checked

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2012
    Messages : 91
    Points : 62
    Points
    62
    Par défaut
    Merci pour ta réponse, j'ai tout changé par des "getElementById" mais ça ne change rien...
    En fait, quand j'appuie sur valider, la valeur du bouton radio est "cartebleue" alors que je n'ai rien checked. Puis ensuite, il y a le message d'alerte "pas bon".

  4. #4
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut
    as tu mis des id à la place ?

    On peut voir le nouveau code ?

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2012
    Messages : 91
    Points : 62
    Points
    62
    Par défaut
    Citation Envoyé par SpaceFrog Voir le message
    as tu mis des id à la place ?

    On peut voir le nouveau code ?
    Voila ^^ et merci pour ton aide
    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
    function formAbonnement()
    {
    	var xhr = null;
       	if(window.XMLHttpRequest)
       	{ // Firefox et autres
         	xhr = new XMLHttpRequest();
       	}
       	else
       	{
    	    if(window.ActiveXObject) 
    		{// Internet Explorer
         		try
    			{
    	   			xhr = new ActiveXObject("Msxml2.XMLHTTP");
    	 		}
    			catch (e)
    			{
    	   			xhr = new ActiveXObject("Microsoft.XMLHTTP");
    			}
        	} 
    		else 
    		{// XMLHttpRequest non supporté par le navigateur
    			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
    			xhr = false;
        	}
       	}
    	var requete = "";
    	requete += "proposition="+document.getElementById('proposition').value+"&choix="+document.getElementById('choix').value;
    	xhr.open( 'POST', 'modele/abonnement.php',  true);
    	if(document.getElementById('choix').checked)
    	{
    		document.getElementById("formAbo").submit();
    	}
    	else
    	{
    		alert("pas bon");
    		return false;	
    	}
    	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xhr.send(requete);
    }
    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
    <form name="formAbo" action="index.php?page=abonnement&action=souscrire" method="POST" id="formAbo" onsubmit="formAbonnement();return false;">
    		<table border>				
    			<tr>
    				<td></td>
    				<td>Abonnement 1 mois<input type="radio" id="proposition" name="proposition" value="1" /></td>
    				<td> 25.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 3 mois<input type="radio" id="proposition" name="proposition" value="2" /></td>
    				<td> 70.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 6 mois<input type="radio" id="proposition" name="proposition" value="3" /></td>
    				<td> 150.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 9 mois<input type="radio" id="proposition" name="proposition" value="4" /></td>
    				<td> 250.00 Euros</td>
    			</tr>
    			<tr>
    				<td></td>
    				<td>Abonnement 12 mois<input type="radio" id="proposition" name="proposition" value="5" /></td>
    				<td> 350.00 Euros</td>
    			</tr>             
    		</table>
     
      	Moyen de paiement : </br>
      	<input type="radio" id="choix" name="choix" value="cartebleue"><img src="images/CB.jpg" height="128px" width="128px" ALIGN="middle"></br>
    	<input type="radio" id="choix" name="choix" value="paypal"><img src="images/paypal.jpg" height="128px" width="128px" ALIGN="middle"></br>
    	<input type="radio" id="choix" name="choix" value="cheque"><img src="images/cheque.png" height="128px" width="128px" ALIGN="middle"></br>
    	</br>
    	<input type="button" value="Valider" onclick="formAbonnement()" name="formAbo"/>
    	</form>

  6. #6
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut


    Un id DOIT ÊTRE UNIQUE sur une page !!!!!

  7. #7
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2012
    Messages : 91
    Points : 62
    Points
    62
    Par défaut
    A bon ? Et comment je fais pour récupérer la proposition et le choix alors ?

  8. #8
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 665
    Points
    66 665
    Billets dans le blog
    1
    Par défaut
    tu n'a pas lu ma réponse précédente

    il faut donc boucler sur la collection et teste l'indice

  9. #9
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2012
    Messages : 91
    Points : 62
    Points
    62
    Par défaut
    A d'accord, merci beaucoup !

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

Discussions similaires

  1. Changer la couleur d'un bouton radio checké
    Par 123quatre dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 02/07/2009, 22h01
  2. Réponses: 0
    Dernier message: 14/11/2008, 12h35
  3. test sur boutons radio
    Par ph_anrys dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 08/05/2008, 17h16
  4. Déterminer le bouton radio checked
    Par cyberdevelopment dans le forum Langage
    Réponses: 8
    Dernier message: 25/07/2006, 16h36
  5. probleme bouton radio checked
    Par coachbob44 dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 06/04/2005, 10h02

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