2010-06-02 5 views
0

Quelle est l'instruction jquery correcte pour remplacer les commentaires "// Besoin magique" ci-dessous afin que les balises d'image soient masquées ou non cachées en fonction des réponses AJAX?Comment masquer une balise d'image basée sur une réponse ajax?

<!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>JQuery</title> 
<style type="text/css"> 
    .isSolvedImage{ 
     width: 68px; 
     height: 47px; 
     border: 1px solid red; 
     cursor: pointer; 
    } 

</style> 
<script src="_js/jquery-1.4.2.min.js" type="text/javascript"> </script> 

</head> 
<body> 

<div id='true1.txt' class='isSolvedImage'> 
    <img src="_images/solved.png"> 
</div> 

<div id='false1.txt' class='isSolvedImage'> 
    <img src="_images/solved.png"> 
</div> 

<div id='true2.txt' class='isSolvedImage'> 
    <img src="_images/solved.png"> 
</div> 

<div id='false2.txt' class='isSolvedImage'> 
    <img src="_images/solved.png"> 
</div> 

<script type="text/javascript"> 

$(function(){ 
    var getDivs = 0; 

    //iterate div with class isSolvedImage 
    $("div.isSolvedImage").each(function() { 
     alert('div id--'+this.id); 
     // send ajax requrest 
     $.get(this.id, function(data) { 
      // check if ajax response is 1 
      alert('div id--'+this.url+'--ajax response--'+data);    
      if(data == 1){ 
       alert('div id--'+this.url+'--Unhiding image--'); 
       //Needed magic 
       //Show image if data==1 
      } 
      else{ 
       alert('div id--'+this.url+'--Hiding image--'); 
       //Needed magic 
       //Hide image if data!=1 
      } 
     }); 

     }); 
}); 
</script> 
</body> 
</html> 

Répondre

1
$('#imageId').hide(); //to hide... 
$('#imageId').show(); //to show... 

ou

$('img').hide(); //to hide all image... 
$('img').show(); //to show all image... 


$(function(){ 
    var getDivs = 0; 

    //iterate div with class isSolvedImage 
    $("div.isSolvedImage").each(function() { 
     alert('div id--'+this.id); 

     var img = $(this).find('img'); 

     // send ajax requrest 
     $.get(this.id, function(data) { 
      // check if ajax response is 1 
      alert('div id--'+this.url+'--ajax response--'+data);    
      if(data == 1){ 
       alert('div id--'+this.url+'--Unhiding image--'); 
       //Needed magic 
       //Show image if data==1 
       img.show(); 
      } 
      else{ 
       alert('div id--'+this.url+'--Hiding image--'); 
       //Needed magic 
       //Hide image if data!=1 
       img.hide(); 
      } 
     }); 

     }); 
}); 
+0

Hmmmmmmmmmmmmmm ... mis à jour/copié à nouveau? :( – jAndy

+0

qu'est-ce qui donne? Huh ?? – Reigel

+0

bien que ce soit, nevermind.bonne réponse – jAndy

0
$('#true1.txt img').hide(); 

Où true1.txt est l'ID de la div contenant l'image que vous souhaitez cacher.

+2

Je pense que vous vouliez dire '$ ('# true1.txt img'). Hide() , n'est-ce pas? –

+0

@Igor: Oui, merci! –

0

Je pense que la méthode la plus simple est $ ('. Target'). Hide(); et $ ('. Target'). Show();

vous pouvez en lire plus ici:
.hide()
.show()
EDIT: Je suppose que devrait avoir fait refresh avant de répondre.

1
$(this).find('img').show(); 

ou

$(this).find('img').hide(); 
0

$('div[id="' + $this.url + '"]').show();

et

$('div[id="' + $this.url + '"]').hide();

Questions connexes