【问题标题】:Update HTML page after jquery.click在 jquery.click 之后更新 HTML 页面
【发布时间】:2016-11-01 21:22:25
【问题描述】:

我有一个 onclick 函数,它基本上只返回排序后的数据,如下所示:

$(document).ready(function () {
    $(".feedbackClick").click(function () {
       $.post("/Analyze/GetSortedByFeedback")
              .done(function (data) {
                  var sellers = $('<table />').append(data).find('#tableSellers').html();
                  $('#tableSellers').html(sellers);
              });
       });
    });
});

这就是我在 jquery 发布后尝试更新的表格的样子:

<table id="tableSellers" class="table table-striped jambo_table bulk_action">
    <thead>
       <tr class="headings">
            <th class="column-title"><h4><i class="fa fa-user" style="text-align:center"></i> <span>Username</span></h4> </th>
            <th class="column-title"> <h4><span class="glyphicon glyphicon-tasks salesClick" aria-hidden="true"></span></h4></th>
            <th class="column-title"><h4><i class="fa fa-star feedbackClick"></i></h4></th>

       </tr>
    </thead>
    <tbody>
       @foreach (var item in ViewBag.rezultati)
       {
             <tr>
                 <td><a href="http://ebay.com/usr/@item.StoreName" target="_blank">@item.StoreName</a></td>
                 <td>
                    <b>
                    @item.SaleNumber
                    </b>
                 </td>
                 <td><b>@item.Feedback</b></td>                                                   
             </tr>
       }

    </tbody>
</table>

点击基本上只是获取结果并更新 HTMl 中的表格...

有人可以帮帮我吗?

编辑:

这个当前方法不起作用...我触发了事件但是没有任何反应...Action中的代码被正确调用了,但是没有显示结果...

编辑 2:

这是.done后数据对象的内容:

System.Collections.Generic.List`1[WebApplication2.Controllers.ResultItem]

编辑 3:

这是动作:

public List<ResultItem> GetSortedByFeedback()
{
    return lista.OrderByDescending(x => x.Feedback).ToList();
}

Edit4 这是 Alexandru 发帖后的数据:

Array[100]

现在我可以做:

data[0].Feedback

这会在控制台中输出:

61259

【问题讨论】:

  • 你确定$('&lt;table /&gt;') 吗?
  • 不,这就是我发布问题的原因。我不确定数据对象是什么样子的,它的内容是什么......:/
  • 请在done函数中使用console.log(data)
  • 你的 .find('#tableSellers') 没有意义 - 你不能 .find() 一些尚未添加到 DOM 的东西。您只需要.done(function (data) { $(someElement).html(data); } 其中someElement 是页面首次呈现时存在的元素
  • 伙计们,我已经用数据对象的内容更新了我的问题,有人可以调查一下吗?

标签: javascript jquery html asp.net asp.net-mvc


【解决方案1】:

请使用这个:

public JsonResult GetSortedByFeedback()
{
   var list=lista.OrderByDescending(x => x.Feedback).ToList();
   return Json(list);
}

如果你的方法是GET,请使用这个:

public JsonResult GetSortedByFeedback()
{
   var list=lista.OrderByDescending(x => x.Feedback).ToList();
   return Json(list,JsonRequestBehavior.AllowGet);
}

那么请使用这个:

 .done(function (data) {
      $('#tableSellers tbody').empty();
      $.each(data,function(i,item){
           var tr='<tr><td><a href="http://ebay.com/usr/'+item.StoreName+'" target="_blank">'+item.StoreName+'</a></td><td><b>'+item.SaleNumber+'</b></td><td><b>'+item.Feedback+'</b></td></tr>';
           $('#tableSellers tbody').append(tr);//append the row
      });
  });

【讨论】:

  • 以及在ajax中将json转换为html的部分?
  • @AlexandruMihai 我现在如何获取 .done 中的数据并更新 HTML ?
  • @User987,你能在使用我的代码后显示你的data吗? console.log(data)。这有助于我更新您的表格。
  • @AlexandruMihai 看起来像这样 :Array[100] ,现在可以访问参数,如 data[0].Feedback... 等等...我现在如何重建数据并更新html?
  • 它不仅像一个魅力,而且很漂亮!谢谢@AlexandruMihai !!!你是老兄! ! ! :)
【解决方案2】:

您正在尝试做的实际上是将 JSON 数据附加到 HTML 元素,这当然不会按预期工作。

考虑使用jQuery Templates 之类的模板引擎。您将能够编译 HTML 模板并在需要时使用它来呈现数据。例如:

var markup = "<li><b>${Name}</b> (${ReleaseYear})</li>";

// Compile the markup as a named template
$.template( "movieTemplate", markup );

$.ajax({
  dataType: "jsonp",
  url: moviesServiceUrl,
  jsonp: "$callback",
  success: showMovies
});

// Within the callback, use .tmpl() to render the data.
function showMovies( data ) {
  // Render the template with the "movies" data and insert
  // the rendered HTML under the 'movieList' element
  $.tmpl( "movieTemplate", data )
    .appendTo( "#movieList" );
}

【讨论】:

    【解决方案3】:

    试试这样的:

    $(document).ready(function () {
        $("body").on("click",".feedbackClick",function() {//delegate the click event
           $.get("/Analyze/GetSortedByFeedback",function(data) {
                      var sellers = $(data).find('#tableSellers').html();//find the table and take the html
                      $('#tableSellers').html(sellers);//append the html
                  });
        });
    });
    

    注意:您需要从 ajaxed 页面返回 html(在您的情况下)

    来自@Alexandru 的部分回复,您可以执行以下操作

    public JsonResult GetSortedByFeedback()
    {
       var list=lista.OrderByDescending(x => x.Feedback).ToList();
       return Json(list,JsonRequestBehavior.AllowGet);
    }
    

    js:

     $(document).ready(function () {
            $("body").on("click",".feedbackClick",function() {//delegate the click event
               $.get("/Analyze/GetSortedByFeedback",function(data) {
                         $('#tableSellers tbody').empty();//empty the table body first
                         $.each(data,function(i,item){//loop each item from the json
                          $('#tableSellers tbody').append('<tr><td><a href="http://ebay.com/usr/'+item.StoreName+'" target="_blank">'+item.StoreName+'</a></td><td><b>'+item.SaleNumber+'</b></td><td><b>'+item.Feedback+'</b></td></tr>');//build and append the html
    });
                      });
            });
        });
    

    【讨论】:

    • 我收到一个错误:语法错误,无法识别的表达式:System.Collections.Generic.List`1[WebApplication2.Controllers.ResultItem]
    • 这意味着您没有从 ajax 返回 html,或者您的 asp 页面有错误
    • 由于某种原因,点击前只显示 1 个结果而不是 100 个初始结果
    • @User987 抱歉将 html 更改为追加,更新了我的答案
    猜你喜欢
    • 2013-09-09
    • 2018-06-11
    • 2011-11-26
    • 2013-10-22
    • 2013-02-24
    • 1970-01-01
    • 2019-09-24
    • 2015-09-24
    • 2012-02-08
    相关资源
    最近更新 更多