recuperation de variable impossible

Par AaFrederic Husson, il y a 8 ans


bonjour

je souhaite recuperer "exp" et "dest" dans cette div:

<div id="list_profil" class="chat_profils"> <?php $sql2 = "SELECT dest FROM chat WHERE exp='".$_SESSION["pseudo"]."' GROUP BY dest"; $req2 = mysql_query($sql2) or die('Erreur SQL !<br>'.$sql2.'<br>'.mysql_error()); while($data2 = mysql_fetch_array($req2)) { $sql3 = mysql_query("SELECT sexe FROM membres WHERE pseudo='".$data2['dest']."'") or die (mysql_error()); list($sexe) = mysql_fetch_row($sql3); if ($sexe == 'Homme') { echo '<A data-exp="'.$_SESSION['pseudo'].'" data-dest="'.$data2['dest'].'" style="margin-bottom: -15px; color: #007ba2;" class="chat_profils_homme" href="/fiche_cli/affiche_mess.php?exp='.$_SESSION['pseudo'].'&dest='.$data2['dest'].'">'.$data2['dest'].'</a>'; } else { echo '<A data-exp="'.$_SESSION['pseudo'].'" data-dest="'.$data2['dest'].'" style="margin-bottom: -15px; color: #f40e63;" class="chat_profils_homme" href="/fiche_cli/affiche_mess.php?exp='.$_SESSION['pseudo'].'&dest='.$data2['dest'].'">'.$data2['dest'].'</a>'; } } ?> </div>

j'ai essayé ceci:

var exp = $('.chat_profils_homme A').attr('data-exp'); var dest = $('.chat_profils_homme A').attr('data-dest'); alert(dest);

et aussi

var exp = $('#list_profil A').attr('data-exp'); var dest = $('#list_profil A').attr('data-dest'); alert(dest);

mais rien y fait

soit c'est toujours le même que je recupere soit c'est undefined

15 réponses

Pierrot01, il y a 8 ans
qqchose comme ça ;)
$('a[class="chat_profils_homme "]').data('exp');
@plus
Pierre
AaFrederic Husson, il y a 8 ans

toujours pas apparment ça me retourne le 1er élément de la liste

vu que l'expediteur ne change pas j'ai cette solution

var exp = $('#list_profil a').data('exp'); var dest = (url.substring(url.lastIndexOf("=")+1));

mais je comprend pas pourquoi ça marche pas

Pierrot01, il y a 8 ans

je suis désolé, mais ce que je t'ai donné fonctionne parfaitement.

$('a[class="chat_profils_homme"]').data('exp');

je viens de tester en direct et j'ai ben le résultat voulu.

@plus

Pierre

Pierrot01, il y a 8 ans

pour preuve : ici

AaFrederic Husson, il y a 8 ans

Je disais pas que ça fonctionne pas mais chez moi ça fonctionne pas, j'ai vu que j'ai deja data-exp et data-dest
donc j'ai modifié à data-exp2 et data-dest-2

ma fonction je l'ai modifié comme ça

$("body").on('click', '.chat_profils_homme',function(event) { event.preventDefault(); var afficheMess = document.getElementById('affiche_mess'); var id_refresh = $('#affiche_mess div:first').attr('id'); clearInterval(interval); var url = $(this).prop('href'); $.ajax({url:url}).done(function(data) { $("#affiche_mess").html(data); afficheMess.scrollTop = afficheMess.scrollHeight; }); var dest $('a[class="chat_profils_homme"]').data('dest2'); var exp $('a[class="chat_profils_homme"]').data('exp2'); $.ajax({ url: '/fiche_cli/ajoute_mess.php?exp=' + exp + '&dest=' + dest, // Ton fichier ou se trouve ton chat success: function(retour){ $('#chat_ecrire').html(retour); // rafraichi toute ta DIV "bien sur il lui faut un id " } }); interval = setInterval(function() {update();}, 1000); });

du coup je suis redirigé vers la page appelé au lieu de passer en ajax

Pierrot01, il y a 8 ans

ça marche ça ?

var dest $('a[class="chat_profils_homme"]').data('dest2'); var exp $('a[class="chat_profils_homme"]').data('exp2');

j'aime meiux ça :

var dest = $('a[class="chat_profils_homme"]').data('dest2'); var exp = $('a[class="chat_profils_homme"]').data('exp2');

@plus

Pierre

AaFrederic Husson, il y a 8 ans

oui effectivement j'avais pas fait attention mais toujours pareil:

j'ai ces liens

<a class="chat_profils_homme" style="margin-bottom: -15px; color: #007ba2;" href="/fiche_cli/affiche_mess.php?exp=alf233&amp;dest=Admin" data-dest2="Admin" data-exp2="alf233">Admin</a> <a class="chat_profils_homme" style="margin-bottom: -15px; color: #007ba2;" href="/fiche_cli/affiche_mess.php?exp=alf233&amp;dest=alf233" data-dest2="alf233" data-exp2="alf233">alf233</a> <a class="chat_profils_homme" style="margin-bottom: -15px; color: #007ba2;" href="/fiche_cli/affiche_mess.php?exp=alf233&amp;dest=alf235" data-dest2="alf235" data-exp2="alf233">alf235</a>

que je clique sur le un le deux ou le trois ça me retourne la même chose:

<input name="dest" type="hidden" value="Admin"> <input name="exp" type="hidden" value="alf233">
Pierrot01, il y a 8 ans

normal, y a plusieur <a class="chat_profils_homme" donc il te retourne le premier ou un tableau

si tu veux qu'il soit unique, il faut mettre un id.

@plus

Pierre

Lartak, il y a 8 ans

Bonjour.
Pour commencer, stockes ton élément dans une variable :

var $chatHomme = $('a.chat_profils_homme'); $chatHomme.on('click', function(e) { e.preventDefault(); var $link = $(this), exp = $link.data('exp'), dest = $link.data('dest'); // ... });

De cette manière, tu évites de faire trop d'intéractions avec le DOM, si un jour tu changes le sélecteur au niveau HTML tu auras plus vite de le changer au niveau Javascript et ça t'évite de prendre le risque de faire une erreur de frappe entre plusieur définitions de variables.

Pierrot01, il y a 8 ans

Salut,

j'vois vraiment pas ou est le problème :D :D !!!!!!
la preuve encore ici

@pluche

Pierre

Pierrot01, il y a 8 ans

J'ai du temps à perdre, j'ai une grosse migration en cours :D
donc, je surveille :D

<style> #aff { border-width:1px; border-style:solid; border-color:black; } </style> <h2> Cliquer sur les liens </h2> <div> <a href="#" class="chat_profils_homme" data-exp="data exp1!" data-dest="data dest1!" >blabla 1</a></br> <a href="#" class="chat_profils_homme" data-dest="dat aexp2!!" data-dest="data dest2!!">blabla 2</a></br> <a href="#" class="chat_profils_homme" data-exp="data exp3!!!" data-dest="data dest3!!!">blabla 3</a></br> <a href="#" class="chat_profils_homme" data-dest="data exp4!!!!" data-dest="data dest4!!!!">blabla 4</a></br> <a href="#" class="chat_profils_homme" data-exp="data exp5!!!!!" data-dest="data dest5!!!!!">blabla 5</a></br> <a href="#" class="chat_profils_homme" data-dest="data exp6!!!!!!" data-dest="data dest6!!!!!!">blabla 6</a></br> <a href="#" class="chat_profils_homme" data-exp="data exp7!!!!!!!" data-dest="data dest7!!!!!!!">blabla 7</a></br> <a href="#" class="chat_profils_homme" data-dest="data exp8!!!!!!!!" data-dest="data dest8!!!!!!!!">blabla 8</a></br> </div> <div id="aff"> </div> <script> $("a.chat_profils_homme").on('click',function(e){ e.preventDefault(); $("#aff").empty().append('dest = '+$(this).data('dest') + ' exp = '+$(this).data('exp')); // alert($(this).data('exp')); // alert($(this).data('dest')); }) </scrpit>

et ca fonctionne très bien

test : ICI

@pluche et bon courage.

Pierre

betaWeb, il y a 8 ans

Salut,

Tu as une minute liste de nodes, il faut boucler dessus afin de récupérer ce que tu souhaites pour chacun des éléments.

Pierrot01, il y a 8 ans

salut,
à priori, il a pas besoin de boucler, puisqu'il veux les éléments au click ;)

@plus.

Pierre

AaFrederic Husson, il y a 8 ans

j'ai pensé faire plus simple recharger le tchat complet mais apres un certain nombre de changement de discution ça commence à ramer

$("body").on('click', '.chat_profils_homme',function(event) { event.preventDefault(); var url = $(this).prop('href'); $("#fiche-1").load(url); });
AaFrederic Husson, il y a 8 ans

comme ça seras mieux...

$(".chat_profils_homme").on('click',function(event) { event.preventDefault(); var url = $(this).prop('href'); $("#fiche-1").load(url); });