0

到目前为止,我脚本中的所有内容似乎都很完美。但是通过 Json 插入和检索的数据没有被插入到我的 div 中。任何有敏锐眼光的人都可以看到任何问题。

<div id="commentaddid"></div>

AJAX

 <script>
    $(document).ready(function(){
    $("form#mycommentform").submit(function(event) {
    event.preventDefault();
    var streamidcontent = $(this).children('#streamidcontent').val();
    var content = $(this).children('#content').val();

    $.ajax({
    type: "POST",
    url: "comment_add.php",
    cache: false,
    dataType: "json",
    data: { streamidcontent: streamidcontent, content: content}, 
    success: function(data){  
    $('#commentaddid').append('<div class="stream_comment">"+content+"</div>');
    }
    });
    return false
    });
    });
    </script>

我工作的主要状态 AJAX。

<script>
$(document).ready(function(){
$("form#myform").submit(function(event) {
event.preventDefault();
var content = $("#toid").val();
var newmsg = $("#newmsg").val();

$.ajax({
type: "POST",
url: "insert.php",
cache: false,
dataType: "json",
data: { toid: content, newmsg: newmsg }, 
success: function(response){ 
 $("#newmsg").val(""); 
$("#homestatusid").html("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div class'delete' style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr>"+newmsg+"<div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');clearTimeout(streamloop);swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'><a title='See who likes "+response['first']+" "+ response['middle']+" "+response['last']+"s status' href='include/likes.php?streamitem_id="+response['streamitem_id']+"' /></a></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'><table width=100%><tr><td valign=top width=30px><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><td valign=top align=left><div class='stream_comment_inputarea'><input id='addcomment' type='text' name='content' style='width:100%;' class='input_comment' placeholder='Write a comment...'  onkeyup='growcommentinput(this);' autocomplete='off' onkeypress=\"if(event.keyCode==13){addcomment("+response['streamitem_id']+",this.value,'comment_list_"+response['streamitem_id']+"',"+response['id']+",'"+response['first']+" "+ response['middle']+" "+response['last']+"');this.value='';}\"><br/></div></div>");
}
});
return false
});
});
</script>
4

3 回答 3

2

您将数据获取为data,但将其附加为content(实际上是您发送的数据)。将您的代码更改为

$('#commentaddid').append('<div class="stream_comment">'+data+'</div>');
于 2012-08-26T18:58:17.210 回答
2

(注意:也许你不想替换' 的内容).appenddivdiv

这段代码是错误的 - 你不匹配单引号和双引号:

$('#commentaddid').append('<div class="stream_comment">"+content+"</div>');

应该:

$('#commentaddid').append('<div class="stream_comment">'+content+'</div>');

...实际上,它应该不止于此。

JSON 返回的值data是一个复杂的对象,您应该对其进行格式化(在 PHP 中Content-Type为 JSON 发出 a 也是谨慎的做法):

content = " " + data.name + "评论:" + ...

于 2012-08-26T18:58:34.360 回答
0
$('#commentaddid').append('<div class="stream_comment">'+data.content+'</div>');
于 2012-08-26T18:59:01.977 回答