duanqian2368 2016-10-26 17:40
浏览 66
已采纳

jQuery on click在while循环中淡入隐藏元素

I have a form in while loop which activates when I click on Reply button. This I can do simply but its in while loop and requires a unique ID. With my code its working till now but it works only for the first result. When I click on the other results nothing happens. Even though I have already assigned a unique ID its not working. My code is given below:

jQuery part:

$(document).ready(function() {
    $("#reply").on('click', 'a.click', function() {
        var rowid = $(this).attr("data-rowid");
        $(".reply-comment-holder[data-rowid='" + rowid + "']").fadeToggle(800),
        $(this).toggleClass(".reply-comment-holder[data-rowid='" + rowid + "']");
    });
});

PHP HTML part:

 <?php while($fetch_cmts = $get_cmtq->fetch()){ extract($fetch_cmts); ?>
    <div id="reply"><a href="javascript:;" data-rowid="<?php echo $cmt_id?>" class="click show-reply-box">Reply</a></div>
    <div class="reply-comment-holder" data-rowid="<?php echo $cmt_id?>" style="display:none;">
              <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="full-width cmtform" id="cmt_form_id_<?php echo $cmt_id?>">
                <input type="hidden" name="status_id" value="<?php echo $cmt_id; ?>" id="cmtsid_<?php echo $cmt_id?>" />
                <textarea name="comment" placeholder="Give a reply..." class="reply-comment-field commentarea" id="replycomment_<?php echo $cmt_id?>"></textarea>
              </form>
            </div>
<?php } ?>

Please help with jQuery/Ajax.

  • 写回答

1条回答 默认 最新

  • doukong1391 2016-10-26 17:58
    关注

    HTML

    <div class="reply"><a href="javascript:;" data-rowid="1" class="click show-reply-box">Reply</a></div>
    <div class="reply-comment-holder" data-rowid="1" style="display:none;">
              <form method="post" action="#" class="full-width cmtform" id="cmt_form_id_1">
                <input type="hidden" name="status_id" value="1" id="cmtsid_1" />
                <textarea name="comment" placeholder="Give a reply..." class="reply-comment-field commentarea" id="replycomment_1"></textarea>
              </form>
            </div>
    <div class="reply"><a href="javascript:;" data-rowid="2" class="click show-reply-box">Reply</a></div>
    <div class="reply-comment-holder" data-rowid="2" style="display:none;">
              <form method="post" action="#" class="full-width cmtform" id="cmt_form_id_2">
                <input type="hidden" name="status_id" value="2" id="cmtsid_2" />
                <textarea name="comment" placeholder="Give a reply..." class="reply-comment-field commentarea" id="replycomment_2"></textarea>
              </form>
            </div>  
    

    javascript

    $(document).ready(function() {
        $(".reply").on('click', 'a.click', function() {
            var rowid = $(this).attr("data-rowid");
            $(".reply-comment-holder[data-rowid='" + rowid + "']").fadeToggle(800),
            $(this).toggleClass(".reply-comment-holder[data-rowid='" + rowid + "']");
        });
    });
    

    jsbin is listed in comments above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮