dongqiu9018 2013-10-07 18:52
浏览 38
已采纳

获得兄弟姐妹jquery / ajax的孩子的价值?

I'm currently trying to make a ajax comment function work once a user clicks "open comments".

Currently I'm getting data back from my php script and the status of the ajax call is "200 OK" so it definetely works but I'm just unable to get the correct value for the current comment which has been clicked on in order to post it to the php script.

What I'm asking is how do I get the value of the ".posted_comment_id" class and then how do I load the data which is returned into the ".commentView" class?

jQuery/AJAX:

$(".closedComment").click(function(){
    var $this = $(this);
    $this.hide().siblings('.openComment').show();
    $this.siblings().next(".commentBox").slideToggle(); 

        $.ajax({
         type: "POST",
         url: "http://example.dev/comments/get_timeline_comments", 
         data: {post_id: $this.siblings().next(".commentBox").find(".posted_comment_id").val()},
         dataType: "text",  
         cache:false,
         success: 
              function(data){
                $this.closest(".commentView").load(data);
              }
         });

    return false;
});

HTML:

<div class="interactContainer">
    <div class="closedComment" style="display: none;">
        <a href="#" class="floatLeft rightMrgn">open comments</a>
    </div>
    <div class="openComment" style="display: block;">
        <a href="#" class="floatLeft rightMrgn">close comments</a>
    </div>
    <div class="commentBox floatLeft" style="display: block;">
        <form action="http://example.com/comments/post_comment" method="post" accept-charset="utf-8">
          <textarea name="comment" class="inputField"></textarea>
          <input type="hidden" name="post" value="13">
          <input type="hidden" name="from" value="5">             
          <input type="hidden" name="to" value="3"> 
          <input type="submit" name="submit" class="submitButton">
        </form> 
          <div class="commentView"></div>   
          <div class="posted_comment_id" style="display:none;">13</div>  
    </div>
</div>
  • 写回答

2条回答 默认 最新

  • drzfnr0275 2013-10-07 18:56
    关注

    Replace .val by .html or .text. This will return the innerHTML of the element.

    data: {
        post_id: $this.siblings().next(".commentBox").find(".posted_comment_id").text()
    }
    

    You might need to convert the string to an integer to make it work.

    If the query selector fails, this selector might do the job instead:

    $this.parent().find(".posted_comment_id")
    

    To add the returned data on your webpage, use the success handler. Here's an example of how it's done:

    success: function(json) {
            // Parse your data here. I don't know what you get back, I assume JSON
            var data = JSON.parse(json),
                content = data.whatever_you_want_to_print;
    
            // Assuming your selector works, you put in in the element using .html
            $this.closest(".commentView").html(content);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题