在jquery中将div追加到div $(函数(){ $(“.commentbox.btnReply”)。单击(函数(){ $(this.hide(); var id=$(this.attr(“id”).split(“-”)[1] var strDiv=“”; $(“#容器-”+id).html(strDiv); }); var i=1; $(“.commentbox”)。在(“单击”,“.btnSave”,函数()上{ var itemId=$(this.attr(“id”).split(“-”)[1] var txt=$(this.parent().find(“.txtCmnt”).val(); var divid=itemId+“-”+i; i++; 警报(“i”+i); 警报(“replypostid”+divid); $.post(“Handler/Topic.ashx”{ 答复:txt,, id:itemId },函数(数据){ 警报(数据); var str=“”; $(“#replytopost”+divid).append(数据); $(“#容器-”+itemId).append(str); }) }); });

在jquery中将div追加到div $(函数(){ $(“.commentbox.btnReply”)。单击(函数(){ $(this.hide(); var id=$(this.attr(“id”).split(“-”)[1] var strDiv=“”; $(“#容器-”+id).html(strDiv); }); var i=1; $(“.commentbox”)。在(“单击”,“.btnSave”,函数()上{ var itemId=$(this.attr(“id”).split(“-”)[1] var txt=$(this.parent().find(“.txtCmnt”).val(); var divid=itemId+“-”+i; i++; 警报(“i”+i); 警报(“replypostid”+divid); $.post(“Handler/Topic.ashx”{ 答复:txt,, id:itemId },函数(数据){ 警报(数据); var str=“”; $(“#replytopost”+divid).append(数据); $(“#容器-”+itemId).append(str); }) }); });,jquery,Jquery,我想将post函数返回的数据附加到一个动态创建的div中,该div被附加到另一个div中 jsiddle链接是 警报(数据)正在打印正确的输出,但未附加带有数据的div 已编辑 如果我单击第一个保存按钮,它将给出replypostid1-1,然后 当我单击第二个按钮时,警报将给出replypostid2-2而不是replypostid2-1 您正在创建一个字符串,然后尝试将其附加到该字符串,就像它存在于DOM中一样。相反,请尝试以下方法: $(function() { $(".comm

我想将post函数返回的数据附加到一个动态创建的div中,该div被附加到另一个div中

jsiddle链接是

警报(数据)正在打印正确的输出,但未附加带有数据的div

已编辑

如果我单击第一个保存按钮,它将给出replypostid1-1,然后 当我单击第二个按钮时,警报将给出replypostid2-2而不是replypostid2-1


您正在创建一个字符串,然后尝试将其附加到该字符串,就像它存在于DOM中一样。相反,请尝试以下方法:

$(function() {
    $(".commentbox .btnReply").click(function() {
        $(this).hide();
        var id = $(this).attr("id").split("-")[1]
        var strDiv = "<input type='text' class='txtCmnt' id='txtReply-" + id + "' /> <input type='button' class='btnSave' value='Save' id='btnSave-" + id + "' /> ";
        $("#container-" + id).html(strDiv);
    });
    var i = 1;
    $(".commentbox").on("click", ".btnSave", function() {
        var itemId = $(this).attr("id").split("-")[1]
        var txt = $(this).parent().find(".txtCmnt").val();
        var divid = itemId + "-" + i;

 i++;
            alert("i" + i);
        alert("replypostid" + divid);

        $.post("Handler/Topic.ashx", {
            reply: txt,
            id: itemId
        }, function(data) {
            alert(data);
            var str = "<div id='replytopost" + divid + "'></div>";
            $("#replytopost" + divid).append(data);

            $("#container-" + itemId).append(str);


        })

    });
});  
$(“#容器-”+itemId)。追加(
$('')
.attr('id','replytopost'+divid)
.append(数据)
);
编辑

我认为依靠Javascript中的计数器来处理回复计数不是一个好的解决方案。如果刷新页面,则该计数器将丢失/重置

如果这个应用程序是数据库驱动的,那么您应该能够从数据库中获得当前回复的数量,并具有更好的数据一致性(通过ajax调用传递),可能作为json对象。这将允许您避免在JS中保留和增加该计数器

$("#container-" + itemId).append(
   $('<div/>')
      .attr('id', '#replytopost'+divid)
      .append( data )
);
$.post(“Handler/Topic.ashx”,{reply:txt,id:itemId},函数(数据){
//假设json对象像{“id”:1,“text”:“曾经的数据现在等于什么”}
var reply=JSON.parse(数据);
$(“#容器-”+itemId)。追加(
$('')
.attr('id','#replytopost'+itemId+'uu'+reply.id)
.append(reply.text)
);
});
您正在调用。在尚不存在的元素上追加(数据);这只是一串东西


我认为这也不是创建新元素的好方法。您应该使用
document.createElement()
element.appendChild()
或一些jquery技术。

更新:尝试以下代码:

$.post("Handler/Topic.ashx", { reply: txt, id: itemId }, function(data) {
   //Assuming a json object like { "id": 1, "text": "What ever data equals now" }
   var reply = JSON.parse( data );
   $("#container-" + itemId).append(
      $('<div/>')
         .attr('id', '#replytopost'+itemId+'_'+reply.id)
         .append( reply.text )
   );
});
它将失败。以下是您的操作方法:

$("#replytopost" + divid)
//创建一个div
var str=$(“”).attr(“id”,“replytopost”+divid)
//访问它
str.append(数据)
//把它附加到任何东西上
附体

这类错误不止一个。如果你把它们都修好了,它应该会起作用。

你可以先切换以下各项。不能附加到不存在的元素上

// Create a div
    var str = $("<div>").attr("id", "replytopost"+divid)
//Access it
    str.append(data)
//Append it to anything
str.appendTo("body")
var-str=”“;
$(“#容器-”+itemId).append(str);//这必须首先添加
$(“#replytopost”+divid).append(数据);

Handler/Topic.ashx的完整url是什么?我无法在JSFIDLE上测试,因为$.post没有从那里执行。看起来您还没有将尝试选择的
div
添加到DOM中。您需要将
div
附加到DOM中,或者将
数据
附加到
str
变量中,即$(str);str.appendTo(“#容器-”+itemId);给出错误:-对象不支持此属性或方法我重写了代码(没有$.post内容)。它在上面。在这里尝试:好的,它可以工作,但我已经更新了我的问题,请看一看。我正在尝试创建类似于此网站的功能,其中主题可以有答案,每个答案都可以有评论。根据我们给你的输入,你应该能够访问此网站。只要重复同样的事情并尝试一下。你应该自己做一些尝试,只有当你失败时,你才可以在这个网站上发布一个问题。我已经更新了我的问题,请看一下。这是你给出的代码works@prerna-为您编辑答案。我相信你会更幸运地改变你的方法。
$("#replytopost" + divid)
// Create a div
    var str = $("<div>").attr("id", "replytopost"+divid)
//Access it
    str.append(data)
//Append it to anything
str.appendTo("body")
var str = "<div id='replytopost"+ divid + "'></div>";
$("#container-" + itemId).append(str); // this has to be added first
$("#replytopost" + divid).append(data);