【问题标题】:Update data in div correctly AJAX (ASP.NET MVC)正确更新 div 中的数据 AJAX (ASP.NET MVC)
【发布时间】:2017-09-09 05:13:27
【问题描述】:

我有 div,我通过 AJAX 在页面加载时显示数据

$(document).ready(function () {
    email_update();
});

function email_update() {
    $.ajax({
        url: '@Url.Action("EmailsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
            var email = result;
            // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++) {
                var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' +
                    '<b style="margin-left: 10px;">' +(i + 1) +
                    '<b style="margin-left:20px;">' + result[i].Email + '</b>'+
                    '<b>' +
                    '<b style="margin-left: 20px;">' +
                    result[i].Name +
                    '</b>' +
                    '</div>';
                $(".email_list").append(emailHTML);
            }
        }
    });
}

我有一个模式窗口,我可以在其中输入数据,当我单击create 按钮时,我需要用新数据更新这个 div。现在我用上面的代码来做。但它重复数据。我需要删除旧的并填充新的。或者更新。

我该怎么做?

【问题讨论】:

  • 把 $(".email_list").empty() 放在你的 while 之前
  • 谢谢老兄!@Webbanditten

标签: javascript jquery css asp.net ajax


【解决方案1】:

我可能误解了这个问题,但我认为你需要将他移到你的 for 循环之外。

$(".email_list").append(emailHTML);

【讨论】:

    【解决方案2】:

    请试试下面的代码,我会工作的。它将删除您旧的 Div 详细信息。 如果有任何困难,请告诉我。

     function email_update() {
    $.ajax({
        url: '@Url.Action("EmailsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
      var emailHTML = "";
            $(".email_list").text = "";
    
            var email = result;
            // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++) {
              emailHTML   = '<div style="margin-left: 25px; margin-top: 10px;>' +
                    '<b style="margin-left: 10px;">' +(i + 1) +
                    '<b style="margin-left:20px;">' + result[i].Email + '</b>'+
                    '<b>' +
                    '<b style="margin-left: 20px;">' +
                    result[i].Name +
                    '</b>' +
                    '</div>';
                $(".email_list").append(emailHTML);
            }
        }
    });
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多