[英]When loading an array of photos, how can I make a unique comment button function for each imageID?
html
HTML
<textarea required=required name ="comment" id="comment"></textarea><br/>
<input type="hidden" id="image" name ="image" value="<?php echo $images[$i]['imageID']?>" />
<input type=button id=postComment value="Post Comment">
commentImage.js $(document).ready(function(){ commentImage.js $(document).ready(function(){
$('#postComment').click(function(){
$.ajax({
type:'post',
url:'commentImage.php',
data:{newComment:$('#comment').val(), postID:postID},
success:function(data){
var data = JSON.parse(data);
var comment = makeComment(data['user'], $('#comment').val(), data['date']);
$('#commentsBock').prepend(comment);
$('#comment').val('');
}
})
})
})
function makeComment(user, comment, date){
var comment = '<div><div>'+user+'</div><div>'+comment+'</div><div>'+date+'</div><div>';
return comment;
}
function getComments(){
}
commentImage.php commentImage.php
<?php
session_start();
echo $_POST['comment'];
echo $_POST['image'];
include_once('mysql.php');
...
?>
#comment
is a unique ID. #comment
是一个唯一的ID。 If you use it more than one time in your HTML, don't wait for it to work properly, it won't ! 如果您在HTML中多次使用它,请不要等待它正常工作,它不会!
Just use your PostID like id="comment_"+PostID
to create unique identifiers. 只需使用您的PostID,如
id="comment_"+PostID
即可创建唯一标识符。 And use $("input[id^='comment_']")
to select all IDs which id start with comment_
(that's what ^=
does) 并使用
$("input[id^='comment_']")
来选择id以comment_
开头的所有ID (这就是^=
做的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.