2012-06-10 23 views
1

我檢索9個圖像縮略圖從數據庫中,每一個駐留在自己的div像這樣:Jquery - 如何從中選擇下一個元素?

<div class="post"> 

<div class="post_desc"></div> 
<div class="post_title"></div> 
<a href="#" class="linktopost">**<img src="img/thumb.png" class="thumb-img"/>**</a> 
<form> 
**<input type="hidden" class="postid" value="'.$post_id.'" />** 
</form> 

</div> 

使用jQuery我想張貼在隱藏的輸入字段被點擊縮略圖時的價值,但我正在努力正確地選擇它。輸入值隨9個圖像中的每一個而變化。

這是我在多大程度上得到了與Jquery的:

$(".thumb-img").click(function(){ 

    $.post("inc/fullpost.php", {postid: ##########.val()}, 
     function(output){ 
      $("#gotpostid").html(output).show(); 
     }).fail(function(x,y,z){ 
      $("#gotpostid").html(x + "<br />" + y + "<br />" + z) 
     }); 

}); 

那麼,如何正確選擇駐留在同一個包含類的圖像縮略圖的輸入字段中的值?

+0

它是設備罩起來,IMG或.thumb-IMG? – Chandu

+0

.thumb-img ..... – crm

回答

1

你可以使用data而不是hidden元素。刪除hidden元素,並嘗試這樣的:

標記:

<img src="img/thumb.png" class="thumb-img" data-postid="'.$post_id.'"/> 

腳本:

$(".thumb-img").click(function(){ 
    $.post("inc/fullpost.php", {postid: $(this).data('postid')},function(output){ 
      $("#gotpostid").html(output).show(); 
     }).fail(function(x,y,z){ 
      $("#gotpostid").html(x + "<br />" + y + "<br />" + z) 
     }); 
}); 
+1

+1不需要使用隱藏的輸入,謝謝。 – crm

1

試試這個:( '帖子ID')。

$(".thumb-img").click(function(){ 
    var postId = $(this).closest(".post").find(".postid").val(); 
    $.post("inc/fullpost.php", {postid: postId}, 
     function(output){ 
      $("#gotpostid").html(output).show(); 
     }).fail(function(x,y,z){ 
      $("#gotpostid").html(x + "<br />" + y + "<br />" + z) 
     }); 

}); 
+0

你知道'#gotpostid'是什麼嗎?在這個問題中'next()'有什麼意義? – undefined

+0

你能詳細說明你的問題嗎? – Chandu

+0

儘管與問題無關,但是#gotpostid是fullpost.php輸出顯示的div。我發送postid並將與該帖子有關的所有數據發回給我。 – crm

0

請嘗試以下

$(yourImage).parent()發現得到(0)

1
$(".linktopost").click(
function(){ var ValueToPost= $(this).next("input").val(); 

$.post("inc/fullpost.php", {postid: ValueToPost}, 
    function(output){ 
     $("#gotpostid").attr('type', 'text').val(output); 
    }).fail(function(x,y,z){ 
     $("#gotpostid").val(x + "<br />" + y + "<br />" + z) 
}); 



}); 
0

好吧,我不知道你是否意識到,而是選擇一價值,你正在做的事實上是在#gotpostid元素中設置值。

要選擇的值,這是方法:

$value = $('#gotpostid').val(); 

如果要設置它,然後:

$('#gotpostid').val($value); 
相關問題