2012-06-10 2 views
1

나는 데이터베이스에서 9 이미지의 썸네일을 검색하고, 각과 같이 자체 사업부에 있습니다 :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 개의 이미지 각각으로 바뀝니다.

$(".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) 
     }); 

}); 

그래서 내가 어떻게 제대로 이미지의 축소판과 같은 포함하는 클래스에있는 입력 필드의 값을 선택합니까 :

이 내가 jQuery로 가지고 얼마나 걸리나요?

+0

이는 방법입니다 값을 선택하려면? – Chandu

+0

.thumb-img ..... – crm

답변

1

당신 hidden 요소 대신 data을 사용할 수 있습니다. 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

이을 시도해보십시오. ('. postid를').

$(".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(); 

당신이 다음을 설정하는 경우 :이 .cover-IMG 또는 .thumb-IMG

$('#gotpostid').val($value); 
관련 문제