ajax响应正在显示javascript而不是隐藏它

时间:2018-06-20 18:08:47

标签: javascript php jquery ajax

我正在构建一个评论系统,其中具有以下功能:删除主要帖子,删除评论,删除回复,编辑主要帖子,编辑评论,编辑回复,阅读更多/阅读少于250个字符的帖子。因此,我现在处于对评论的回复帖子进行编辑的阶段,除此以外,其他所有功能均正常运行,当我单击时,我需要查看具有“阅读更多/阅读更少”功能的回复帖子,因此要在以后进行此操作ajax响应我需要在用于编辑回复的php脚本中粘贴此功能的javascript代码,因此当我进行.html(data)时,我会在希望显示响应的范围内看到javascript代码!请帮忙 !我用相同的脚本进行注释,并以相同的方式将javascript代码放置在编辑该注释的php页面中,但是我没有看到javascript行!以下是显示正在发生的情况的图片以及下一个我的脚本:

    // THIS TAKES CARE OF THE EDIT FEATURE OF THE REPLY ON BOARD_COMMENT PAGE 
$(document).on("click", ".board_reply_edit_button", function() {
    // this will select the form in which is contained the edit button
    var editBoardButtonAttribute = $(this).attr("id");
    var editBoardButtonIdArray = editBoardButtonAttribute.split("-");
    var editBoardButtonId = editBoardButtonIdArray[0];

    $("#"+editBoardButtonId+"-formBoardReplyEdit").toggle();
    $("#"+editBoardButtonId+"-spanBoardReplyEdit").toggle();

    // if the cancel button is clicked, this happens
    $(document).on("click", ".board_cancel_button", function() {
        // this will select the form in which is contained the edit button
        var cancelBoardAttribute = $(this).attr("id");
        var cancelBoardButtonIdArray = cancelBoardAttribute.split("-");
        var cancelBoardButtonId = cancelBoardButtonIdArray[0];

        $("#"+cancelBoardButtonId+"-formBoardReplyEdit").hide();
        $("#"+cancelBoardButtonId+"-spanBoardReplyEdit").show();

    });

    // if the edit button is clicked we send this ajax call
    $(document).on("click", ".board_edit_save_button", function(e) {
        e.preventDefault();

        // this will select the form in which is contained the edit button
        var saveBoardAttribute = $(this).attr("id");
        var saveBoardButtonIdArray = saveBoardAttribute.split("-");
        var saveBoardButtonId = saveBoardButtonIdArray[0];

        var editBoardTextareaVal = $("#"+saveBoardButtonId+"-textareaBoardReplyEdit").val();

        url = "widgets/edit_board_comment_reply.php";

        if (editBoardTextareaVal === "") {
            CustomSending("This post can't be left blank")
            setTimeout(function () {
                $("#sending_box").fadeOut("Slow");
                $("#dialogoverlay").fadeOut("Slow");
            }, 2000);
            // this makes the scroll feature comes back
            setTimeout(function(){
                $("body").css("overflow", "auto");
            }, 2001);
        } else {

            $.ajax({
                url: url, 
                method: "POST",
                data: {
                    reply_id: saveBoardButtonId,
                    board_reply_textarea: editBoardTextareaVal
                },
                beforeSend: function() {
                    CustomSending("Sending...");
                },
                success: function(data){
                    $("#sending_box").fadeOut("Slow");
                    $("#dialogoverlay").fadeOut("Slow");
                    // this makes the scroll feature comes back

                    $("body").css("overflow", "auto");

                    $("#"+saveBoardButtonId+"-spanBoardReplyEdit").html(data); //// THIS IS THE KEY LINE
                    $("#"+saveBoardButtonId+"-formBoardReplyEdit").hide();
                    $("#"+saveBoardButtonId+"-spanBoardReplyEdit").show();
                }
            });
        }
    });

});

This is the image, key line is in the codes at the end

这是edit_board_comment_reply.php文件:

  <?php 
require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';


if(isset($_POST['reply_id'], $_POST['board_reply_textarea'])) {
    $reply_id = (int)$_POST['reply_id'];
    $board_reply_textarea = mysql_prep($_POST['board_reply_textarea']);

    // INSERT into table
    $query  = "UPDATE board_comment_reply_table ";
    $query .= "SET reply = '$board_reply_textarea' ";
    $query .= "WHERE reply_id = $reply_id";
    $result = mysqli_query($connection, $query);

    // now we select the updated board post
    $query2 = "SELECT * FROM board_comment_reply_table ";
    $query2 .= "WHERE reply_id = $reply_id ";
    $result2 = mysqli_query($connection, $query2);
    confirm_query($result2);
    $result_array = mysqli_fetch_assoc($result2);


}
echo nl2br($result_array['reply']);

?>

<script>
// This takes care of the board comment Continue Reading feature ---------------------------------------------------------
$(".reply_content_span").each(function(){
    var boardReplyPostThis = $(this);
    var boardPostText = $(this).text();
    var boardPostLength = boardPostText.length;
    var boardIdAttribute1 = $(this).attr("id");
    var boardIdAttributeArray1 = boardIdAttribute1.split("-");
    var boardPostId = boardIdAttributeArray1[0];
    var boardPostUserId = boardIdAttributeArray1[1];

    if(boardPostLength > 250) {
        var boardPostTextCut = boardPostText.substr(0, 250);
        boardReplyPostThis.text(boardPostTextCut+"...");
        boardReplyPostThis.append('<a class="board_read_more_link board_reply_read_more" id="'+boardPostId+'-readMoreComment">Read More</a>');
    } else {
        boardReplyPostThis.text(boardPostText);
    }


    $("body").on("click", ".board_reply_read_more", function(e){
        e.preventDefault();
        boardReplyPostThis.text(boardPostText);
        boardReplyPostThis.append('<a class="board_read_more_link board_reply_read_less">Read Less</a>');
    });

    $("body").on("click", ".board_reply_read_less", function(e){
        e.preventDefault();
        boardReplyPostThis.text(boardPostTextCut+"...");
        boardReplyPostThis.append('<a class="board_read_more_link board_reply_read_more">Read More</a>');
    });

});

</script>

这是html代码:

<span class="comment_content_span" id="<?php echo $board_comment_id_number;?>-spanBoardCommentEdit"><?php echo nl2br($board_comment_text);?></span>
                    <form action="" method="post" class="board_comment_edit_form" id="<?php echo $board_comment_id_number;?>-formBoardCommentEdit">
                        <textarea rows="2" name="board_comment_edit_textarea" class="board_comment_edit_textarea" id="<?php echo $board_comment_id_number;?>-textareaBoardEdit"><?php echo $board_comment_text;?></textarea>
                        <input type="submit" value="Edit" class="board_edit_save_button" id="<?php echo $board_comment_id_number;?>-saveBoardCommentEdit"/>
                        <input type="button" value="Cancel" class="board_cancel_button" id="<?php echo $board_comment_id_number;?>-cancelBoardCommentEdit"/>
                    </form>

2 个答案:

答案 0 :(得分:0)

找到解决方案!

我在edit_board_comment_reply.php中添加了这一行,而不是脚本标记之间的javascript

<script src="js/board.js"></script>

答案 1 :(得分:-1)

似乎“数据”中附加的javascript exitsts是从您的php脚本发送回去的。首先,您应该尝试定义ajax-post的“ dataType”属性,因为输出是由jquery根据该属性进行预处理的。

在那之后,您可以尝试从resonse-html中仅获取内容分区,然后附加整个内容而不是整个结果。

例如这样的$(body).append($(data).find('#contentID'))。