Ajax删除所有子元素并读取更新后的值
Ajax removing all childelemets and readding the updated values
我正在使用ajax请求向数据库发布新消息。页面在加载时自动从数据库加载元素。我想删除元素,并在用户发布帖子将当前最多的帖子添加到列表顶部时重新添加它们,但这不起作用。我不知道为什么。没有控制台错误,但不会删除它们。
加载ajax调用
$.ajax({
url : "/getposts/",
type : "POST",
dataType: "json",
data : {
csrfmiddlewaretoken: '{{ csrf_token }}',
},
success : function(json) {
document.getElementById('output').innerHTML = (json['message']);
for (var index = 0; index < json['user_posts'].length; index++) {
var div_make_badassness = document.createElement('div');
div_make_badassness.id = json['user_posts'][index][3];
document.getElementById('post_section').appendChild(div_make_badassness);
document.getElementById(json['user_posts'][index][3]).innerHTML = "<div id=" + json['user_posts'][index][3] + ">" + "Title:" + json['user_posts'][index][1] + "<br>" + json['user_posts'][index][0] + " Chomps: " + json['user_posts'][index][2] + "</div>" ;
}
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText);
document.getElementById('output').innerHTML = "Request Failed.";
}
});
关于提交ajax调用
$.ajax({
url : "/makepost/",
type : "POST",
dataType: "json",
data : {
csrfmiddlewaretoken: '{{ csrf_token }}',
username: username,
post_title: post_title,
post_text: post_text,
},
success : function(json) {
document.getElementById('output').innerHTML = (json['message']);
var post_section = document.getElementById("post_section");
for (var index = 0; index < post_section.length; index++) {
post_section.removeChild(post_section.childNodes[index]);
}
div标签
<div id ='post_section'>
</div>
for (var index = 0; index < json['user_posts'].length; index++) {
console.log(json['user_posts'][index][3] + json['user_posts'][index][1] + json['user_posts'][index][2]);
var div_make_badassness = document.createElement('div');
div_make_badassness.id = json['user_posts'][index][3];
document.getElementById('post_section').appendChild(div_make_badassness);
document.getElementById(json['user_posts'][index][3]).innerHTML = "<div id=" + json['user_posts'][index][3] + ">" + "Title:" + json['user_posts'][index][1] + "<br>" + json['user_posts'][index][0] + " Chomps: " + json['user_posts'][index][2] + "</div>" ;
}
因为您使用的是jQuery,所以我只在jQuery中执行。
// Will remove any html in "post_section" and add in the new posts
function updatePostSection(user_posts) {
var post_section = $('#post_section');
post_section.html(''); // Remove all html contents inside
for(var i = 0; i < user_posts.length; i++) {
var div = $('<div>');
div.attr('id', user_posts[i][3]);
div.html("Title:" + user_posts[i][1]);
post_section.append(div);
}
}
在成功ajax函数中,您可以执行以下操作:。。。
// ...
success : function(json) {
updatePostSection(json['user_posts']);
},
// ...
然后,假设json是相同的结构,您应该能够将其用于getposts
和makepost
ajax调用。
更新:有一些方法可以优化这一点,所以您只向dom写入一次,但这只是一个例子。
相关文章:
- 以字符串形式读取量角器测试中的更新输入
- 无法更新 Kendo-grid 中的值 - 无法读取未执行的“数据”属性
- Ajax删除所有子元素并读取更新后的值
- 指令链接中的范围变量不会更新到指令控制器使用 Angular 读取它时
- 无法通过 nodejs 读取更新文件的属性
- AJAX 请求无法读取更新的会话/全局变量
- SAPUI5/OPENUI5:将字符串绑定到 JSON 模型并自动更新更大的字符串(读取 [对象%20Object])
- Angularjs:将从文件读取的内容更新到文本区域
- 可以使用JavaScript FileReader接口更新和读取文件吗
- Windows Eyes和NVDA使用jquery读取更新的dom内容..可能的
- "无法读取属性'选项'未定义的“;尝试更新数据网格时的EasyUI
- 如何从浏览器中读取和写入excel文件,然后将值更新到数据库
- 用于快速读取、不频繁更新/插入的文件或数据库
- 如何更新一个变量在成功的短信读取插件
- 是在创建、销毁和更新时触发requestStart还是仅在读取时触发
- Azure移动服务Javascript库和更新-未读取任何方法
- 读取Json并从mustache更新模板
- 在目录更新后读取HTML5文件系统
- NVDA在动态更新后读取aria隐藏的内容
- JQuery读取XML文件,然后在atrr=更新时重定向