按下后停止按钮动作

时间:2017-05-10 16:54:59

标签: javascript jquery

我有一个JS函数,可以加载某个答案的注释。我们的想法是调用API函数并返回注释。 问题是每次按下按钮都会加载注释,同样的注释。 我想要一些在你按评论后,你不能加载更多的评论。我虽然stopPropagation会做到这一点,但什么都没发生。

当我打电话给我按下评论按钮后,是否可以关闭我的#showarea

<div class="post-button clearfix">
                <div class="post-button clearfix">
                    <button class="btn icon-chat show-textarea" title="Add a comment on this answer" type="button" data-answer="{$answer['publicationid']}">Comment</button>
                    <div class="textarea">
                        {include file="comment_form.tpl"}
                    </div>
                </div>
            </div>

comment_form.tpl

$('.comment-form').hide();
var commentsFetched = false;

$("body").on("click", ".show-textarea", function(){
    if (commentsFetched) { // check the flag
        return;
    }
    $.getJSON("/controller/api/comments/comment.php", {
        answerid : answerid
    }, function (data) {
        commentsFetched = true;
        $.each(data, function(i, comment) {
            console.log(comment);
            $('.comment-form').append('<article class="tweet-data">' +
                '<div class="comment-items">' +
                '<div class="qa-c-list-item  hentry comment" id="c3574">' +
                '<div class="asker-avatar">' +
                '<a>' +
                '<img width="40" height="40" src="' +
                comment.user_photo +
                '"></a>' +
                '</div>' +
                '<div class="qa-c-wrap">' +
                '<div class="post-button">' +
                '<button name="" onclick="" class="btn icon-flag" title="Flag this comment as spam or inappropriate" type="submit">flag</button>' +
                '<button name="" class="btn icon-answers" title="Reply to this comment" type="submit">reply</button>' +
                '</div>' +
                '<span class="qa-c-item-meta">' +
                'commented' +
                ' 1 day' +
                ' ago' +
                ' by ' +
                '<a style="display: inline" href="" class="qa-user-link url nickname">' +
                comment.username +
                '</a> ' +
                '<span class="qa-c-item-who-points"> ' +
                '<span class="qa-c-item-who-points-pad">(</span><span class="qa-c-item-who-points-data">140</span><span class="qa-c-item-who-points-pad"> points)</span> ' +
                '</span> ' +
                '</span> ' +
                '</span> ' +
                '<div class="qa-c-item-content" style="color: #2d2727; font-size: 13px"> ' +
                '<a name="3574"></a><div class="entry-content">' +
                comment.body +
                '</div> ' +
                '</div> ' +
                '</div> <!-- END qa-c-item --> ' +
                '</div> ' +
                '</div>');
        });
    });

    $('.comment-form').show();
});

$("body").on("click", ".textarea-ok, .textarea-cancel", function(){
    commentsFetched = false;
    $('.comment-form').hide();
});

1 个答案:

答案 0 :(得分:2)

设置一个标志,以便知道您是否已经提取了评论。

var commentsFetched = false;
$("#showarea").click(function() {
    if (commentsFetched) { // check the flag
       return;
    }

    $.getJSON("/controller/api/comments/comment.php", {
        answerid : answerid
    }, function (data) {
        commentsFetched = true; // set the flag
        $.each(data, function(i, comment) {
    ...

您应该禁用该按钮,为您的用户提供可视指示,不再需要加载评论