如何使我的jQuery喜欢功能在项目中的每个帖子上都能正常工作
P粉111927962
P粉111927962 2023-09-16 11:56:50
0
1
1129

大家好。我正在设计一个类似社交媒体平台的平台,它有帖子、点赞和评论系统。我使用 jquery 来喜欢,每当我想发短信并且想喜欢一个帖子时,只有第一个帖子可以工作,但其他帖子的喜欢系统无法工作。我该如何解决它。 这是包含帖子和 jquery 代码的 index.php。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>post</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="font/css/all.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>
            $(document).ready(function(){

                $("#likings").click(function(){
                    var name = $("input").val();
                    $.post("like.php", {
                        sugess: name
                    }, function(data, status){
                        $("#like").html(data);
                    });
                });
            });
    </script>
</head>
<?php
include('conn.php');
?>
<body>
    <?php
    $sql = "SELECT * FROM post";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($result)){
        $id = $row['id'];
        $name = $row['name'];
        $image = $row['image'];
        $action = $row['action'];
        $date = $row['date'];
        $time = $row['time'];
        $sqlcount = "SELECT * FROM likes WHERE postid=$id";
        $resultcount = mysqli_query($conn, $sqlcount);
        $count = mysqli_num_rows($resultcount);
        echo '
        <center>
            <div class="post">
                <div class="up">
                    <div>
                        <img class="img" src="img/'.$image.'">
                    </div>
                    <div class="uptext"><span class="name">'.$name.' </span><span class="des"> '.$action.'
                        <br>'.$date.' at '.$time.'</span></div>
                    <div>
                        ...
                    </div>
                </div>
                <img src="img/'.$image.'">
                <div class="liking">
                    <div class="like">
                        <i class="fa fa-thumbs-up" aria-hidden="true" style="text-align: left; color: navy;"></i><span id="like">'.$count.'</span>
                    </div>
                    <div>
                        <p>23 comments</p>
                    </div>
                </div>
                <div class="likenow">
                    <div>
                        <input name="id" value="'.$id.'" hidden>
                        <i class="fa fa-thumbs-up" id="likings" aria-hidden="true" style="text-align: left; color: navy;"></i>like
                    </div>
                    <div>
                        <i class="fa-solid fa-message"></i> comment
                    </div>
                </div>
                <p style="text-align: left; margin-left: 20px; font-size: 10pt;">view more comments</p>
                <div class="comments">
                    <p>itz celeb <br> very nice</p>
                    <p>titi kosi <br> so cute</p>
                </div>
                <div class="entercomment">
                    <div>
                        <img class="img" src="img/'.$image.'">
                    </div>
                    <div>
                        <input placeholder="Write a comment..."><i class="fa-solid fa-message" style="margin-left: 20px;"></i>
                    </div>
                </div>
            </div>
        </center>';
    }
    ?>
</body>
</html>

这是喜欢的 php 代码。

<?php
include("conn.php");
$name = $_POST['sugess'];
$sql = "INSERT INTO likes(postid, likes) VALUES('$name', '1')";
$result = mysqli_query($conn, $sql);
$sqlcount = "SELECT * FROM likes WHERE postid=$name";
$resultcount = mysqli_query($conn, $sqlcount);
$count = mysqli_num_rows($resultcount);
echo $count;
?>

那我该如何解决呢?

类似的内容应该适用于每个帖子。我使用了 while 循环,但我仍然遇到同样的问题。

P粉111927962
P粉111927962

全部回复(1)
P粉959676410

你的问题出在选择器上,它是一个ID选择器,使用ID选择器你只能选择一个元素,尝试将其更改为类选择器

<i class="fa fa-thumbs-up" id="likings" aria-hidden="true" style="text-align: left; color: navy;"></i>

<i class="fa fa-thumbs-up likings"  aria-hidden="true" style="text-align: left; color: navy;"></i>

应该可以

<script>
        $(document).ready(function(){

            $(".likings").click(function(){
                var name = $("input").val();
                $.post("like.php", {
                    sugess: name
                }, function(data, status){
                    $("#like").html(data);
                });
            });
        });
</script>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板