我正在嘗試爲每個窗體構建一個按鈕;當用戶點擊它時,具有特定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加載您的表單?我在代碼中看不到任何問題,它應該可以工作。 –