从 php while 循环输出时如何在 dividually 切换 div
how can I toggle a div individually when outputting from a php while loop
我有这个 php 脚本 -
$sql = "SELECT * FROM comments WHERE post_id='$id' ORDER BY com_id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$date = $row['date'];
$mydate = date("M jS g:i a",strtotime($date));
$user = $row['user'];
$comment = $row['comment'];
$reply = $row['reply'];
$comID = $row['com_id'];
echo '<div id="comuser">'.$user.': </div>';
echo '<div id="icomment">'.$comment.'</div>';
echo '<div id="comdate">'.$mydate.'</div>';
echo '<div id="replyBTN">reply</div>';
echo '<form method="post" id="replyForm" action="get_reply.php?reply='.$comID.'">';
echo '<input type="text" id="addReply" name="addReply" placeholder="add reply">';
echo '<input type="submit" name="subCom" value="submit">';
echo '</form>';
}
} else {
echo "<div id='noCom'>no comments..</div>";
}
我想回应 div 'replyBTN' 并使用 jquery 切换表单,这是我的 jquery 点击功能 -
$(document).ready(function(){
$("#replyBTN").click(function(){
$("#replyForm").toggle();
});
});
目前只会在第一条评论中切换第一个回复按钮,所有其他回复按钮将无法在其他评论中使用。为什么 jquery 只切换第一个回复按钮?
我认为这适合作为社区 Wiki;我没有从中获得任何好处,我也不希望获得代表收益。
摘自评论:
The reason it only works for the first one, is because ID's are unique, so jQuery stops searching for more elements once the first one is found, simply because there can't be more elements with the same ID, it's invalid. – adeneo
还有我的
...therefore, use a class – Fred -ii-
我有这个 php 脚本 -
$sql = "SELECT * FROM comments WHERE post_id='$id' ORDER BY com_id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$date = $row['date'];
$mydate = date("M jS g:i a",strtotime($date));
$user = $row['user'];
$comment = $row['comment'];
$reply = $row['reply'];
$comID = $row['com_id'];
echo '<div id="comuser">'.$user.': </div>';
echo '<div id="icomment">'.$comment.'</div>';
echo '<div id="comdate">'.$mydate.'</div>';
echo '<div id="replyBTN">reply</div>';
echo '<form method="post" id="replyForm" action="get_reply.php?reply='.$comID.'">';
echo '<input type="text" id="addReply" name="addReply" placeholder="add reply">';
echo '<input type="submit" name="subCom" value="submit">';
echo '</form>';
}
} else {
echo "<div id='noCom'>no comments..</div>";
}
我想回应 div 'replyBTN' 并使用 jquery 切换表单,这是我的 jquery 点击功能 -
$(document).ready(function(){
$("#replyBTN").click(function(){
$("#replyForm").toggle();
});
});
目前只会在第一条评论中切换第一个回复按钮,所有其他回复按钮将无法在其他评论中使用。为什么 jquery 只切换第一个回复按钮?
我认为这适合作为社区 Wiki;我没有从中获得任何好处,我也不希望获得代表收益。
摘自评论:
The reason it only works for the first one, is because ID's are unique, so jQuery stops searching for more elements once the first one is found, simply because there can't be more elements with the same ID, it's invalid. – adeneo
还有我的
...therefore, use a class – Fred -ii-