我正在尝试为每个窗体构建一个按钮;当用户点击它时,具有特定ID的特定表单将再次显示或隐藏。根据按钮ID显示/隐藏表格
我已经尝试了下面的JavaScript代码,但它不起作用。
这段代码错了吗?还是我错过了什么?有人有另一个想法吗? 在此先感谢。
$(function(){
$('.btn').on('click', function(e){
e.preventDefault();
$(this).next('.form2').show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
$result_posts = $conn -> prepare("SELECT * FROM posts WHERE post_topic=:post_topic ORDER BY DATE(post_date) ASC");
$result_posts -> bindParam(':post_topic',$topic_id);
$result_posts -> execute();
while ($row2 = $result_posts ->fetch(PDO::FETCH_ASSOC))
{
?>
<a class="btn" id="<?php echo $row2['post_id']; ?>"><i class="fa fa-commenting" aria-hidden="true"></i>Comment</a>
<form name="form2" class="form2" id=" <?php echo $row2['post_id']; ?>" style="display:none">
<textarea class="commenting" id="commenting" placeholder="Comment here..." cols="30" rows="5"></textarea>
<input type="submit" class="comment_submit2" value="Submit" >
</form>
<?php } ?>
你有没有在你的片段包括jQuery的 - 你,包括它在你的网页?考虑编辑你的PHP/HTML代码,而不是一个精确代表输出的小型HTML代码片段。 – Santi
我正在使用“jquery-3.1.0.min.js” –
您是否正在通过ajax加载您的表单?我在代码中看不到任何问题,它应该可以工作。 –