提问者:小点点

单击ajax post页面中的按钮时不起作用[重复]


我有一个注释项目,在我的.user__comments__container div中有一个按钮id#see_more_comments,当注释超过4时,该按钮通过jquery post从getcomments-afunc.php文件输出。ajax.php的文件代码是:

<?php
session_start();
include_once '../dbh-inc.php';

if (isset($_SESSION['userid'])) {

    $jobsid = mysqli_real_escape_string($conn, $_POST['jid']);
    $output = "";

    $sql = "SELECT * FROM comments AS c INNER JOIN users AS u ON c.usersId = u.usersId WHERE c.jobsId = '$jobsid' ORDER BY c.cid DESC LIMIT 4;" ;
    $result = mysqli_query($conn, $sql);
    $commentcount = 3;
    if (mysqli_num_rows($result) > $commentcount) {
        while ($row = mysqli_fetch_assoc($result)) {
        $usersid = $row['usersId'];
        $commenterprofilepic = $row['usersProfilePic'];
        $username = $row['usersName'];
        $usersusername = $row['usersUsername'];
        $commentDesc = $row['commentDesc'];
        $commentDate = $row['commentDate'];

                $output .= '<div class="user__comments">
                                <img src="profilepic/'.$commenterprofilepic.'" alt="'.$username.'\'s profile picture" class="profile__thumbnail" onclick="location.href=\'profile?uid='.$usersid.'\'">
                                <div class="comment__bubble">
                                    <div class="comment__box">
                                        <p class="username" onclick="location.href=\''.$usersusername.'\'">'.$username.'</p>
                                        <p class="comment">'.nl2br($commentDesc).'</p>
                                    </div>
                                    <p class="date">'.$commentDate.'</p>
                                </div>
                            </div>';

        }
         $output .= '<div class="see__more__comments" id="see_more_comments">See more comments..</div>';
    }
    echo $output;
}
else {

    header("../../dashboard.php");
    exit();
}

现在,当它通过jquery post调用输出到我的index.php时,它就被注册了,可以在我的浏览器或DOM的elements选项卡上看到。

它通过以下jquery代码输出:

setInterval(() => {
            $.post('includes/ajax-func/getcomments-afunc.php', {
                jid : pageId
            }, function (response) {
                $('.user__comments__container').html(response);
            });
        }, 10000
    );

但是,当我点击时,它会向我的.user__comments__container index.php添加4个注释。它不工作,我试着为它做一个console.log,但它没有输出任何东西,这是点击按钮的代码:

$('#see_more_comments').click(function() {
        commentCount = commentCount + 4;
        $('.user__comments__container').load('includes/ajax-load/seemorecomments-aload.php' , {
            jobsId : pageId,
            newCommentCount : commentCount
        });
        console.log(commentCount);
    });

这是seemoreComments-aload.php的内容:

<?php
session_start();
include_once '../dbh-inc.php';
    
    $jobsid = $_POST['jobsId'];
    $newcommentcount = $_POST['newCommentCount'];

$sql = "SELECT * FROM comments AS c INNER JOIN users AS u ON c.usersId = u.usersId WHERE c.jobsId = '" . $jobsid . "' ORDER BY c.cid DESC LIMIT $newcommentcount;" ;
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $usersid = $row['usersId'];
    $commenterprofilepic = $row['usersProfilePic'];
    $username = $row['usersName'];
    $usersusername = $row['usersUsername'];
    $commentDesc = $row['commentDesc'];
    $commentDate = $row['commentDate'];

        echo '<div class="user__comments">
        <img src="profilepic/'.$commenterprofilepic.'" alt="'.$username.'\'s profile picture" class="profile__thumbnail" onclick="location.href=\''.$usersusername.'\'">
        <div class="comment__bubble">
            <div class="comment__box">
                <div class="username" onclick="location.href=\''.$usersusername.'\'">'.$username.'</div>
                <div class="comment">'.$commentDesc.'</div>
            </div>
            <div class="date">'.$commentDate.'</div>
        </div>
    </div>';
}

在此之前,我有所有getcomments-afunc.php内容,但没有$output,$output被echo替换,在我的index.php.user__comments__container中,它工作得很好。

但是,当我决定向其中添加jquery以便不需要重新加载整个页面时,我让.user__coments__container重新加载自己的目的起作用了,但是按钮不再起作用了。

有人帮忙吗?谢谢!!:(


共2个答案

匿名用户

更改线路

$('#see_more_comments').click(function() {

$(document).on("click","#see_more_comments",function() {

匿名用户

感谢马吉德阿里先生和斯瓦蒂先生,

成功了!!通过使用$(document).on(“click”,“#see_more_comments”,function(){

我能知道为什么$('#name').Click有时不起作用吗??