如何在通话后添加新记录添加元素来更新DOM?如何在Ajax调用(jQuery)之后更新DOM?
我应该在回调中返回新插入的db记录内容,然后使用jquery追加?
如果没有足够的数据来回答问题,请指教。
$(function(){
$('#postentry').submit(function() {
var tyu = $('#tittle_ent').val();
if(tyu.length <= 2)
{alert('Enter More Text!'); return false;}else
{
$.post(
"posts_in.php",
$("#postentry").serialize(),
function(data){
//alert(data);
}
);
return false;
}
});
});
每个记录是这样的:
<div id="post349" class="myclass">
This is some text.
</div>
容易理解。谢谢。 – Codex73
如何返回新添加的记录的ID以便插入? – Codex73
你想从posts_in.php页面返回一个JSON对象,如下所示:'echo json_encode('{“id”:“post349”,“record”:“这是一些文本”}');'' 。然后在你的ajax'success:function()'中,你可以将该对象引用为'data.id'和'data.record'。你还需要将'dataType:'json''添加到你的ajax参数中,以便JQuery知道如何解析传入的数据。 – AlienWebguy