简体   繁体   English

在不刷新的情况下在保存时更新表中的数据 (Ajax/JavaScript)

[英]Update Data in Table on Save with No Refresh (Ajax/JavaScript)

I have the following function that is the event listener function to a button.我有以下功能,它是按钮的事件侦听器功能。 When the button is clicked, this function is called and it should be updating an HTML table.当按钮被点击时,这个函数被调用,它应该更新一个 HTML 表格。 It is doing so properly, but not until the user refreshes the page.这样做是正确的,但直到用户刷新页面。 What must I change on the front-end in order to make the table updates occur as soon as this button is clicked rather than forcing the user to refresh the page?我必须在前端更改什么才能在单击此按钮时立即更新表而不是强制用户刷新页面? Thank you!谢谢!

function dialogWindowSaveAddressBookEditsButton_Click(addressBookObject) {
$.ajax({
    type: "POST",
    url: "/Services/AsyncServices.svc/DialogWindowSaveAddressBookEditsButton_Click",
    contentType: "application/json",
    data: JSON.stringify(addressBookObject),
    success: function (result) {
        if (result.d == false) {
            ...
        }
        else {
            ...
        }
    }
});
}

You have the result from your post request - you need to format it and then set the html table (or whatever component is it) to display that data.您获得了发布请求的结果 - 您需要对其进行格式化,然后设置 html 表(或它的任何组件)以显示该数据。

You are not providing enough information (as of are you using any framework or just pure javascript), so I would suggest you to look into how data binding works for web development.您没有提供足够的信息(因为您使用的是任何框架还是纯 javascript),因此我建议您研究数据绑定如何用于 Web 开发。 https://www.wintellect.com/data-binding-pure-javascript/ https://www.wintellect.com/data-binding-pure-javascript/

If you are using a framework (AngularJs, React, Ember etc) look into how to bind data - it is usually very simple如果您正在使用框架(AngularJs、React、Ember 等),请查看如何绑定数据 - 通常非常简单

When you call an ajax request then you must handle its response and update DOM accordingly.当您调用 ajax 请求时,您必须处理它的响应并相应地更新 DOM。 If the request is returning HTML then you can just render that under any element you want like this:如果请求返回 HTML,那么您可以在您想要的任何元素下呈现它,如下所示:

$('#element).html(response)

If it's returning JSON then you should set values accordingly to your DOM elements by using Jquery:如果它返回 JSON,那么您应该使用 Jquery 根据您的 DOM 元素设置值:

$(“.your-table or td”).text(resp.value)

我通过将以下内容添加到成功属性中解决了该问题:

location.reload(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM