2013-10-08 25 views
1

我有HTML代碼:如何使用jQuery每種方法發佈每個圖像的值?

<div> 
    <span class="image" id="img-1"></span> 
    <span id="src-1">img-src-1</span> 
    <span id="cmd-1">img-cmd-1</span> 
    <span id="h1-1">img-h1-1</span> 
    <span id="h2-1">img-h2-1> 
</div> 

<div> 
    <span class="image" id="img-2"></span> 
    <span id="src-2">img-src-2</span> 
    <span id="cmd-2">img-cmd-2</span> 
    <span id="h1-2">img-h1-2</span> 
    <span id="h2-2">img-h2-2> 
</div> 

和其他增加的div像IMG-3,IMG-4,IMG-5 .....與相關值

,這是我的javascript:

$('.image').each(function() { 
    $.post("../../includes/processes/show-images.php", { 
    width : $(this).parent().width(), 
    img_src : $(this).next().text(), 
    cmd : $(this).next().next().text(), 
    h1 : $(this).next().next().next().text(), 
    h2 : $(this).next().next().next().next().text() 
    }, 
    function(response){ 
    $(this).parent().html(response); 
    }); 
}); 
return false; 

我想爲每個「.image」元素髮布其值,並在「.image」.parent()中返回響應,但我的代碼不起作用。 :-(

有人可以幫我請

回答

3

this$.post的回調函數不指點擊的元素的上下文中的關鍵字,對象緩存:

$('.image').each(function() { 
    var $this = $(this); 
    $.post("../../includes/processes/show-images.php", { 
    // ... 
    }, function(response){ 
     $this.parent().html(response); 
    }); 
}); 

你也可以使用.siblings().eq()方法而不是多次調用.next()方法:

$('.image').each(function() { 
     var $this = $(this), 
      $siblings = $this.siblings(); 
     $.post("../../includes/processes/show-images.php", { 
      width : $this.parent().width(), 
      img_src : $siblings.eq(0).text(), 
      cmd  : $siblings.eq(1).text(), 
      // ... 
     }, function(response){ 
      $this.parent().html(response); 
     }); 
}); 
相關問題