我在头版的php中有foreach循环来获取图像和图像描述,在foreach循环中我有表单,表单用于发送评论,这是首页..
<?php foreach ($photo as $p) : ?>
<div class="photo-box">
<div class="galP photo-wrapper" >
<div data-fungal="<?php echo $p->id; ?>" class='galFun-get_photo'>
<img src="<?php echo $p->thumb; ?>" class='image'>
</div>
</div>
<div class='inline-desc'>
<a href="/gallery/user.php?id=<?php echo $p->userId; ?>">
<?php echo $p->username; ?>
</a>
</div>
<form method="POST" action="" class="form-inline comment-form galForm">
<div class="form-inline">
<input type="hidden" class='photoId form-control' name="photoId" value="<?php echo $p->id; ?>" >
<input type="hidden" class='userId form-control' name="userId" value="<?php echo $session->userId; ?>" >
<textarea cols="30" rows="3" class='comment fun-gal-textarea' name="comment" placeholder="Leave your comment"></textarea>
<button type='button' name='send' class='sendComment'>SEND</button>
</div>
</form>
<div class='new-comm'></div>
<div class='comments-gal' id='comments'>
<div data-id='<?php echo $p->id; ?>' class='getComment'>
<span>View comments</span>
</div>
</div>
</div>
使用ajax我想在点击具有类sendComment的按钮后发送userId,photoId和comment。当我发送第一张图片的评论时,一切都还可以,但是当我尝试发送其他图片的评论时,它无法正常工作。我无法选择特定的输入和textarea来设置正确的值。这是我的jquery
$('body').on('click','.sendComment',function(){
var selector = $(this);
var userId = selector.siblings($('.userId'));
var photoId = selector.siblings($('.photoId'));
var c = selector.siblings($('.comment'));
var comment = $.trim(c.val());
if (comment == "" || comment.length === 0) {
return false;
};
$('#no-comments').remove();
$.ajax({
url: '/testComment.php',
type: 'POST',
data: {comment:comment,userId:userId,photoId:photoId}
}).done(function(result) {
...
}
})
});
此外,我已尽力以各种方式从表单中获取正确的值而没有成功。
这条线
var userId = selector.siblings($('.userId'));
根据https://api.jquery.com/siblings/的说法,不太可能获得正确的输入
.siblings([selector])
选择
包含用于匹配元素的选择器表达式的字符串。
所以这需要是:
var userId = selector.siblings('.userId');
此时你还需要从输入中获取实际值,给出:
var userId = selector.siblings('.userId').val();
var photoId = selector.siblings('.photoId').val();
var c = selector.siblings('.comment');
其余代码按原样。