【发布时间】:2018-07-23 21:56:55
【问题描述】:
我有一个动态表,其中包含一些 userInfo 使用get method ("/api/dashboard/v1")。我有一个模式,onSubmit,它将 userInfo 添加到表 using post method ("/api/adduserInfo/v1"). p>
我使用window.location.replace("http://localhost:3000/") 重新加载页面,但希望“更新表格内容而不重新加载页面”。
我没有得到任何适当的解决方案来解决这些问题。请帮助我解决这些问题。我的代码在下面:
/index.html
<table class="table-bordered mytable">
<thead class="table-head">
<tr>
<th>Name</th>
<th>Email</th>
<th>Status</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody class="table-body mytabBody">
</tbody>
</table>
<div class="col-md-12 text-right">
<button type="button" data-toggle="modal" data-target="#myModal">Update User Information</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Enter User Information</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="text" class="form-control" id="user_name" placeholder="User name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email_id" placeholder="Enter email">
</div>
<div class="form-group">
<input type="text" class="form-control" id="address" placeholder="Enter Address">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" id="myFormSubmit">Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
/modal.js
/* 这些函数用于将新的用户信息保存到表中 */
$(function() {
$('#myFormSubmit').click(function (e) {
e.preventDefault();
var username = $("#user_name").val();
var email = $("#email_id").val();
var address = $("#address").val()
$.ajax({
url: "/api/adduserInfo/v1",
data: {
user_name: username,
email: email,
address: address
},
type: 'post',
dataType: 'html',
success: function(data) {
},
error: function(xhr, status) {
alert("Sorry, there was a problem!");
},
});
window.location.replace("http://localhost:3000/"); //not update only the table content without reloading the page. I want only update the table content not reload the page.
})
});
/table.js
/* 从数据库中获取表内容 */
$(function(){
$.get("/api/menu/v1", function(response) {
//console.log(response);
var html = "";
for(var i = 0; i< response.length ; i++){
html += "<li><a href =''>"+response[i].name+"</a></li>"
}
$('.sidebar-nav').html(html);
});
$.get("/api/dashboard/v1", function (response) {
//console.log(response);
var myhtmlsec= "";
for(var i=0; i<response.data.length; i++) {
myhtmlsec += "<tr class='myTable'>";
myhtmlsec +="<td id='tabUser'>"+response.data[i].user_name+"</td>";
myhtmlsec +="<td id='tabEmail'>"+response.data[i].email+"</td>";
myhtmlsec +="<td id='tabStatus'> </td>";
myhtmlsec +="<td id='tabAddress'>"+response.data[i].address+"</td>";
myhtmlsec +="<td>\
<a href='#' onclick='myEdit(this);return false;' class='table-edit img-size'>\
<img src='image/Edit.png'>\
</img>\
</a>\
<a href='#' onclick='myDelete(this);return false;' class='table-delete img-size'>\
<img src='image/Delete.png'>\
</img>\
</a>\
</td>";
myhtmlsec +="<td class='hide' id='tabId'>"+response.data[i]._id+"</td>";
myhtmlsec +="</tr>"
}
$('.table-body').append(myhtmlsec);
});
});
我尝试了大多数 ajax、jquery、javascript 方法,但仍然没有在不重新加载整个页面的情况下更新表格,在提交模式时也没有关闭
【问题讨论】:
-
删除你的
window.location.replace("http://localhost:3000/");,你的ajax的success结果是什么? -
in success alert("用户信息添加成功");
-
你仍然可以添加
alert() -
只需在下面查看我的答案。 :)
-
再次不更新表格而不重新加载页面。
标签: javascript jquery html ajax node.js