duanmu6752
2013-07-19 08:01
浏览 24

将数据加载到当前div中

I have two divs, and when one is clicked it empties the div. I want to be able to load data into that div once it's been emptied. I've got it so it loads in both, but I only want to load data into the div that's been emptied.

HTML (I have two of these)

<div class="fl person">
    <div class="maintain_size">
        <input type="hidden" name="userSaved" value="<?php echo $user_one['id']; ?>" />
            <img src="<?php echo "http://graph.facebook.com/".$user_one['id']."/picture?type=large"; ?>" class="circle-mask"  />
            <img class="hoverimage" src="images/fire_extinguisher.png" />
            <div class="meta_data">
                <h2><?php echo $user_one['name']; ?></h2>
            </div>
    </div>
</div>

Javascript

<script>
$("div.person img").click(function () {
        $(this).parent("div").fadeOut(1000, function () {
        var saved_id_user_who_voted_val = "<?php echo $_SESSION['id']; ?>";
        var saved_id_user_voted_on_val = $(this).parent('div').find('input:hidden');
        var saved_id_user_voted_on_val = saved_id_user_voted_on_val.val();

        $(this).parent("div").empty();

        // Load in new data
        var current_div = $(this).parent("div");
        $.get('loadNewUser.php', function(data) {
          current_div.html(data);
        });
    });
});
</script>

The data seems to come through fine if I select a div that's not the current one, or I just use an alert to see if the data is there--which it is, but if I try load the data into the current div it just doesn't work. Any ideas? Thanks.

The code that I think is wrong:

$.get('loadNewUser.php', function(data) {
      current_div.html(data);
});

Edit: The div I'm trying to get the data into is: div.person - although, there are 2 of these.

图片转代码服务由CSDN问答提供 功能建议

我有两个div,当点击一个div时,它会清空div。 我希望能够在清空后将数据加载到该div中。 我已经得到它,所以它加载到两者,但我只想将数据加载到已被清空的div。

HTML(我有两个)

 &lt; div class =“fl person”&gt; 
&lt; div class =“maintain_size”&gt; 
&lt; input type =“hidden”name =  “userSaved”value =“&lt;?php echo $ user_one ['id'];?&gt;”  /&gt; 
&lt; img src =“&lt;?php echo”http://graph.facebook.com/".$user_one['id']."/picture?type=large“;?&gt;”  class =“circle-mask”/&gt; 
&lt; img class =“hoverimage”src =“images / fire_extinguisher.png”/&gt; 
&lt; div class =“meta_data”&gt; 
&lt; h2&gt;  &lt;?php echo $ user_one ['name'];  ?&gt;&lt; / h2&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
   
 
 

Javascript < /strong>

<script>
$("div.person img“)。click(function(){
 $(this).parent(”div  “).fadeOut(1000,function(){
 var saved_id_user_who_voted_val =”&lt;?php echo $ _SESSION ['id'];?&gt;“; 
 var saved_id_user_voted_on_val = $(this).parent('div'  ).find('input:hidden'); 
 var saved_id_user_voted_on_val = saved_id_user_voted_on_val.val(); 
 
 $(this).parent(“div”)。empty(); 
 
 //加载 新数据
 var current_div = $(this).parent(“div”); 
 $ .get('loadNewUser.php',function(data){
 current_div.html(data); 
});  
}); 
}); 
&lt; / script&gt; 
   
 
 

如果选择的div不是当前的数据,数据似乎很好 ,或者我只是使用警报来查看数据是否存在 - 它是什么,但如果我尝试将数据加载到当前div中它就不起作用。 有任何想法吗? 谢谢。

我认为错误的代码:

  $。get('loadNewUser.php  ',function(data){
 current_div.html(data); 
}); 
   
 
 

编辑: div I 试图获取数据的是:div.person - 尽管如此,其中有2个。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutale7115 2013-07-19 08:17
    已采纳

    Once you empty the div your 'this' is no longer there. Try just declaring var current_div before you empty.

    var current_div = $(this).parent("div");
    current_div.empty();
    $.get('loadNewUser.php', function(data) {
          current_div.html(data);
    });
    

    See fiddle

    点赞 评论
  • dsjzmrz78089 2013-07-19 08:05

    You need to delegate the event as you are replacing old content with new one

    $(document.body).on('click',"div.person img",function () {
    
    点赞 评论
  • dongzhao5970 2013-07-19 08:09

    Use live() function Example

    $("div.person img").live("click",function () {
    //Your code goes here
    });
    
    点赞 评论

相关推荐 更多相似问题