我正在学习如何使用AJAX和PHP json_encode()
。我返回使用json_encode()
数组作为AJAX POST
响应:json_encode在ajax上打印html块成功,需要咨询
$.post("<?php echo base_url();?>index.php/post/post_insert_ajax/",{
text: post_wall, type:type, entity_id:entity_id, poster_id:poster_id
},
function (data){
var postObj = $.evalJSON(data);
$("ul#post_already").prepend(//something);
$("ul#post_already li:first").slideDown("slow");
$('#post_wall').val("");
});
我使用jquery.json.js爆炸阵列返回,我想显示HTML的这个块分成$("ul#post_already")
<a id='"+postObj.post_id+"' href='board/?p="+postObj.post_id+"' >
"+postObj.post_text+"
</a>
Created on: "+postObj.created_timestamp+"<a class='showCommentBox' id='"+postObj.post_id"' href='#' >Comment</a>
<div id='commentContainer-"+postObj.post_id+"' class='commentContainer' style='display:none;'>
<form class='submit_comment' id='"+postObj.post_id+"' action='#'>
<textarea name='text' id='commentText-"+postObj.post_id+"' class='commentText'></textarea>
<input type='submit' name='submit' value='Post Comment' id='"postObj.post_id"' class='submit_comment' />
<input type='hidden' name='user_id' id='user_id' value='"+postObj.user_id+"' />
</form>
</div>
<ul id='comment_post"+postObj.post_id+"'></ul></li>
我试图把所有这些都放到.prepend()
,但它似乎没有这样工作,我该怎么做才能实现这个目标?非常感谢你。
更新:它会证明,我错过了一些代码的串连象征,所以在此期间我的解决办法是这样的:
block = "<b>"+user_name+"</b> posted on ";
block += "<br/><a id='"+postObj.post_id+"' href='<?php echo base_url();?>board/?p="+postObj.post_id+"' >"+postObj.post_text+"</a>";
block += "<br />Created on: "+postObj.created_timestamp+" <a class='showCommentBox' id='"+postObj.post_id+"' href='#' >Comment</a>";
block += "<div id='commentContainer-"+postObj.post_id+"' class='commentContainer' style='display:none;'>";
block += "<form class='submit_comment' id='"+postObj.post_id+"' action='#'><textarea name='text' id='commentText-"+postObj.post_id+"' class='commentText'></textarea>";
block += "<input type='submit' name='submit' value='Post Comment' id='"+postObj.post_id+"' class='submit_comment' />";
block += "<input type='hidden' name='user_id' id='user_id' value='"+postObj.user_id+"' />";
block += "</form></div><ul id='comment_post"+postObj.post_id+"'></ul></li>";
和成功,我叫HTML:
$("ul#post_already").prepend(HTML);
它现在工作,但我仍然认为这是凌乱的线条,而不是一个最佳做法,有没有任何解决方法来做到这一点?
伟大的人的感谢,我将要再次尝试这一点,并取回在您,谢谢 – littlechad 2011-04-10 17:12:39
不是问题,人。希望它适合你。哦...并且,使用上述技术,您不需要“jquery.json.js”文件来分解数组。上面演示的所有功能都内置在jQuery核心中。 – 2011-04-10 17:22:44
虽然不完全像我想要的那样,但是我因为你指示我在那里获得了新的知识:),无论如何,我怎样才能简化我的更新中的乱码?谢谢 – littlechad 2011-04-10 17:38:47