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

jQuery Discussion :

Supprimer/Ajouter ligne dans des blocs différents


Sujet :

jQuery

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    étudiant
    Inscrit en
    Mai 2017
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : étudiant

    Informations forums :
    Inscription : Mai 2017
    Messages : 42
    Par défaut Supprimer/Ajouter ligne dans des blocs différents
    Bonjour,
    Je suis débutant en js/jquery ce que je voudrais faire c’est
    -supprimer la ligne correspondant lorsqu’on click sur le bouton( -)
    - ajouter une ligne à la fin du bloc lorsqu’on click sur le bouton( +)

    Pour supprimer une ligne j’utilise hide() et ajouter clone()

    Mon problème est le suivant :
    Je voudrais exécuter le code sur plusieurs blocs(div) le souci c’est que lorsque je click sur (-) la ligne disparait sur toute les blocs(div) et pareil lorsque j’ajoute une ligne

    Voici mon code :
    Code html : 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
    <!doctype html>
    <html lang="fr">
    <head>
      <meta charset="utf-8">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
      <title>Suprimer/Ajouter - ligne</title>
    </head>
    <script>
     
             $(document).ready(function(){
             $(".moins").click(function(){
                    $(".line").hide();
        });
            
            
            /* $(".plus").click(function(){
                    var $clone = $('.line').clone();
                    $clone.appendTo('.line ' +"");
        });
            */
     
     
    });
     
     
    </script>
     
    <style>
    .bloc{
    border:1px solid black;
    margin-bottom:5px;
    }
    .line{
    display: inline;
    }
     
    </style>
     
    <body>
    <div class="bloc">
    	<!--ligne 2-->
    	<div class="line">
    		<select name='options'>
    			<option value='option-1'>val 1</option>
    			<option value='option-2'>val 2</option>
    			<option value='option-2'>val 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>nom 1</option>
    			<option value='option-2'>nom 2</option>
    			<option value='option-2'>nom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>prenom 1</option>
    			<option value='option-2'>prenom 2</option>
    			<option value='option-2'>prenom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>date 1</option>
    			<option value='option-2'>date 2</option>
    			<option value='option-2'>date 3</option>
    		</select>
    	</div>
    		<button type="button" class="moins"> <span class="glyphicon glyphicon-minus  "></span></button> <br></br>
     
    	<!--ligne 2-->
    	<div class="line">																								
    		<select name='options'>
    			<option value='option-1'>val 1</option>
    			<option value='option-2'>val 2</option>
    			<option value='option-2'>val 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>nom 1</option>
    			<option value='option-2'>nom 2</option>
    			<option value='option-2'>nom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>prenom 1</option>
    			<option value='option-2'>prenom 2</option>
    			<option value='option-2'>prenom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>date 1</option>
    			<option value='option-2'>date 2</option>
    			<option value='option-2'>date 3</option>
    		</select>
    	</div>	
    	<button type="button" class="moins"> <span class="glyphicon glyphicon-minus"></span></button> <br></br>
    	<button type="button" name="plus" class="plus" >+</button>										
    </div>
     
     
     
    <div class="bloc"> 																										
    	<div class="line">																								
    		<select name='options'>
    			<option value='option-1'>val 1</option>
    			<option value='option-2'>val 2</option>
    			<option value='option-2'>val 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>nom 1</option>
    			<option value='option-2'>nom 2</option>
    			<option value='option-2'>nom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>prenom 1</option>
    			<option value='option-2'>prenom 2</option>
    			<option value='option-2'>prenom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>date 1</option>
    			<option value='option-2'>date 2</option>
    			<option value='option-2'>date 3</option>
    		</select>
    	</div>
    	<button type="button" class="moins"> <span class="glyphicon glyphicon-minus"></span></button> <br></br>	
    	<button type="button" name="plus" class="plus" >+</button>										
    </div>
     
     
    <div class="bloc"> 																										
    	<div class="line">																								
    		<select name='options'>
    			<option value='option-1'>val 1</option>
    			<option value='option-2'>val 2</option>
    			<option value='option-2'>val 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>nom 1</option>
    			<option value='option-2'>nom 2</option>
    			<option value='option-2'>nom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>prenom 1</option>
    			<option value='option-2'>prenom 2</option>
    			<option value='option-2'>prenom 3</option>
    		</select>
    		<select name='options'>
    			<option value='option-1'>date 1</option>
    			<option value='option-2'>date 2</option>
    			<option value='option-2'>date 3</option>
    		</select>
    	</div>	
    	<button type="button" class="moins"> <span class="glyphicon glyphicon-minus"></span></button> <br></br>
    	<button type="button" name="plus" class="plus" >+</button>
     
    </div>
     
    </body>
    </html>

    Merci de votre aide.

  2. #2
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 209
    Par défaut
    Bonjour,
    change la structure de ton document pour n'avoir à faire qu'un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $(this).parent().remove();
    Au passage hide() ne supprime pas du DOM mais masque à l'affichage seulement

  3. #3
    Membre averti
    Femme Profil pro
    étudiant
    Inscrit en
    Mai 2017
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : étudiant

    Informations forums :
    Inscription : Mai 2017
    Messages : 42
    Par défaut
    C’est-à-dire changé la structure du document ? Je n’ai pas compris

  4. #4
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 209
    Par défaut
    C'est surtout qu'il te faut respecter la structure que tu as utilisée mais de façon plus rigoureuse
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <div class="bloc">
      <div class="line">
        <select></select>
      </div>
      <button></button>
    </div>
     
    <div class="bloc">
      <div class="line">
        <select></select>
      </div>
      <button></button>
    </div>
    regarde ton code ce n'est pas toujours le cas.

  5. #5
    Membre averti
    Femme Profil pro
    étudiant
    Inscrit en
    Mai 2017
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : étudiant

    Informations forums :
    Inscription : Mai 2017
    Messages : 42
    Par défaut
    Bonjour,
    J’ai trouvé une solution voici le code :
    Code html : 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
    <!doctype html>
    <html lang="fr">
    <head>
      <meta charset="utf-8">
      <title>Ajouter/Supprimer</title>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    </head>
    <style>
    .bloc{
    border : 1px solid black;
    }
     
    </style>
    <script>
     
    function Supprimer(moins){
            $(moins).parent().remove();//supprimer le parrent 
    }
     
    function Ajouter(){
            $('#first').find(".ligne").last()
                    .after($('#first').find(".ligne").first().clone().show());
    }
    function Ajouter1(){
            $('#second').find(".ligne").last()
                    .after($('#first').find(".ligne").first().clone().show());
    }
     
            
    </script>
     
    <body>
    <div class="bloc" id="first" >
    	<div class="col-md-12">
    		<div class="ligne" style="display:none;"> 			
    			<input type="date" name=""  >	
    			<input type="date" name=""  >	
    			<select name='options'>
    			<option value='option-1'>prenom 1</option>
    			<option value='option-2'>prenom 2</option>
    			<option value='option-2'>prenom 3</option>
    		</select>
    			<button type="button" onclick="Supprimer(this);" class="btn btn-danger btn-xs "> <span class="glyphicon glyphicon-minus"></span></button>
    		</div>
    		<div class="ligne" > 		
    			<input type="date" name=""  >	
    			<input type="date" name=""  >	
    			<select name='options'>
    				<option value='option-1'>prenom 1</option>
    				<option value='option-2'>prenom 2</option>
    				<option value='option-2'>prenom 3</option>
    			</select>
    			<button type="button" onclick="Supprimer(this);" class="btn btn-danger btn-xs "> <span class="glyphicon glyphicon-minus"></span></button>
    		</div>
    		<button type="button" onclick="Ajouter();" name="" class="" style="visibility: visible;">+</button>
     
    	</div></br>
    	<button type="button" class="vld">Valider</button>
    	<p>Vous avez ajouter (nombre de ligne 1-2-3-4 ou 5)</p>
    </div> <br></br>
     
    <div class="bloc" id="second" >
    	<div class="col-md-12">
    		<div class="ligne" style="display:none;"> 			
    			<input type="date" name=""  >	
    			<input type="date" name=""  >	
    			<select name='options'>
    				<option value='option-1'>prenom 1</option>
    				<option value='option-2'>prenom 2</option>
    				<option value='option-2'>prenom 3</option>
    			</select>			
    			<button type="button" onclick="Supprimer(this);" class="btn btn-danger btn-xs "> <span class="glyphicon glyphicon-minus"></span></button>
    		</div>
    		<div class="ligne" > 		
    			<input type="date" name=""  >	
    			<input type="date" name=""  >
    			<select name='options'>
    				<option value='option-1'>prenom 1</option>
    				<option value='option-2'>prenom 2</option>
    				<option value='option-2'>prenom 3</option>
    			</select>			
    			<button type="button" onclick="Supprimer(this);" class="btn btn-danger btn-xs "> <span class="glyphicon glyphicon-minus"></span></button>
     
    		</div>
    		<button type="button" onclick="Ajouter1();" name="" class="" style="visibility: visible;">+</button>		
    	</div> </br>
    	<button type="button" class="vld">Valider</button>
    	<p>Vous avez ajouter (nombre de ligne 1-2-3-4 ou 5)</p>
    </div>
    </body>
    </html>

    J’aimerais limiter le nombre d’ajout à 5 et afficher ce nombre lorsqu'on click sur valider.
    Est-ce que quelqu’un peux m’aider ?
    Merci

  6. #6
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 209
    Par défaut
    qu'est ce t'empêche de tenir à jour un compteur ?

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

Discussions similaires

  1. Réponses: 3
    Dernier message: 18/05/2014, 11h37
  2. Réponses: 1
    Dernier message: 12/03/2013, 13h39
  3. [MySQL] Supprimer 1 ligne dans 2 tables différentes
    Par Gogad dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 23/02/2010, 00h59
  4. [XSL]<b> et </b> dans des blocs if différents ?
    Par Eylir dans le forum XSL/XSLT/XPATH
    Réponses: 6
    Dernier message: 17/08/2009, 09h56
  5. ajouter supprimer une ligne dans table
    Par mans27 dans le forum JSF
    Réponses: 1
    Dernier message: 28/05/2007, 13h56

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