gpt4 book ai didi

javascript - 如何在不使用 ajax、jquery、javascript 重新加载整个页面的情况下更新我的动态 Web 内容

转载 作者:行者123 更新时间:2023-11-28 02:44:39 26 4
gpt4 key购买 nike

我有一个动态表,其中包含一些使用get method ("/api/dashboard/v1")userInfo .我有一个模式,onSubmit,它将 userInfo 添加到表 using post method ("/api/adduserInfo/v1").

我用了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">&times;</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>

/模态.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 方法,但仍然没有在不重新加载整个页面的情况下更新表格,提交模式也没有关闭

最佳答案

您可以像这样将 ajax 的结果附加到您的表中

$(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: 'json',
success: function(data) {
//append the data here just like you do in your other ajax request

var myhtmlsec= "";
for(var i=0; i<data.length; i++) {
myhtmlsec += "<tr class='myTable'>";
myhtmlsec +="<td id='tabUser'>"+data[i].user_name+"</td>";
myhtmlsec +="<td id='tabEmail'>"+data[i].email+"</td>";
myhtmlsec +="<td id='tabStatus'> </td>";
myhtmlsec +="<td id='tabAddress'>"+data[i].address+"</td>";
myhtmlsec +="</tr>";
//and so on...
}
$('.table-body').append(myhtmlsec);
alert("User Information Added Successfully");

},
error: function(xhr, status) {
alert("Sorry, there was a problem!");
},
});
//window.location.replace("http://localhost:3000/"); remove this
})
});

关于javascript - 如何在不使用 ajax、jquery、javascript 重新加载整个页面的情况下更新我的动态 Web 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42130157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com