【问题标题】:Json Response into a table overwritingJson 响应到表覆盖
【发布时间】:2013-10-17 23:39:03
【问题描述】:

我有这个模板,我在其中进行 ajax 调用以获取药品价格,我得到一个数据对象,问题是我无法在表格中显示数据,因为它会覆盖最后一个值和其他问题,它不会让表中的任何其他数据删除所有数据,如 nombre_medicamento 或 presentacion,它会创建一个新表。 我正在使用 Django 和 Python

<script>
 $( document ).ready(function() {
    var data = $("#attendees tr.data").map(function (index, elem) {
    var ret = [];
        $('.inputValue', this).each(function () {
            var d = $(this).val()||$(this).text();
            ret.push(d);
            buscarprecio(d);
     });
     return ret;
});

function buscarprecio(idgrupo) {
      $.ajax({
            url: "/precio_search",
            type: "POST",
            dataType: "json",
            data: {
                grupname: idgrupo,
                csrfmiddlewaretoken: '{{ csrf_token }}'
            },
                success: function (json) {

                var jsonResponse = eval(json);
                var tbl_body = "";
                       var tbl_row = "";
                 $.each(jsonResponse, function(index, element){

                      $.each(this, function(k , v) {
                        tbl_row += "<td>"+ jsonResponse[0]["fields"]["medicamento_precio"]+"      </td>";
                   })
                  tbl_body += "<tr>"+tbl_row+"</tr>";
             })

                     $("#attendees tbody").html(tbl_body);
                 },
             error: function (xhr, errmsg, err) {
                alert(xhr.status + "  Inside error :    " + xhr.responseText);
            }
        });
     }

});
</script>
<table id="attendees" class="attendees">
<tbody>
<thead>
         <tr>
                <th style="text-align: center"><label>Nombre Del Medicamento</label></th>
                <th style="text-align: center"><label>Presentaciòn</label></th>
                <th style="text-align: center"><label>Precio</label></th>
                <th style="text-align: center"><label>Fecha En Que Se Extendio La  Receta</label></th>

          </tr>
            </thead>
             {% if formulario %}
                 {% for post in formulario %}
                 <tr class="data">
                    <td style="text-align: center" class="inputValue"> {{ post.nombre_medicamento_2.id}}</td>
                    <td style="text-align: center"> {{ post.presentacion_3}}</td>
                    <td style="text-align: center"  class="precio"></td>
                    <td style="text-align: center"> {{ post.receta_farmacos }}</td>
                </tr>
            {% endfor %}
        {% endif %}
    </tbody>
 </table>
{% endblock %}

【问题讨论】:

    标签: jquery python ajax django


    【解决方案1】:

    所以你的代码中的问题是你用这行$("#attendees tbody").html(tbl_body);覆盖了表格主体

    您可以使用$().append() 将新行添加到您的表中。

    查看文档here 以获取示例。

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 2017-01-30
      相关资源
      最近更新 更多