简体   繁体   English

如何在循环中将json数据显示为HTML

[英]How to show json data to HTML in a loop

I have to show some json data to html page in a list below is my jquery code 我必须在下面的列表中显示一些json数据到html页面是我的jquery代码

var u_Name, u_Mobile, u_lat, u_lang, driver_ID;

function fetchEnquiry(driverid) {
    jQuery.ajax({
        url: baseurl + "fetchenquiry.php",
        data: 'driverid=' + driverid,
        type: "POST",
        success: function (response) {
            response = $.trim(response);
            if (response == "NO Enquiry") {
                console.log(response);
            } else {
                var enquiryDet = jQuery.parseJSON(response);

                jQuery(enquiryDet).each(function (index, element3) {
                    u_Name = element3.name;
                    u_Mobile = element3.mobile;
                    u_lat = element3.location_lat;
                    u_lang = element3.location_lang;
                    driver_ID = element3.driver_id;
                    $.each(enquiryDet, function (i, item) {
                        $('.notifications_show').html(enquiryDet[i].name);
                    });
                });
            }
        },
        error: function () {}
    });
}

HTML CODE HTML代码

 <div class="notifications_show">

            </div>

It is showing me only one last result. 它只显示最后一个结果。

Please Help! 请帮忙!

On this part: 在这一部分上:

$.each(enquiryDet, function (i, item) {
      $('.notifications_show').html(enquiryDet[i].name);
});

You're replacing the full html of the result element. 您要替换result元素的完整html。

You would need to use .append() to add more values, instead of replacing the current ones. 您将需要使用.append()添加更多值,而不是替换当前值。

Like this: 像这样:

$.each(enquiryDet, function (i, item) {
      $('.notifications_show').append(enquiryDet[i].name);
});

Edit 编辑

As mentioned in the comments, you should add a $('.notifications_show').empty() before the .each to clear the current results in the target div. 如评论中所述,您应该在.each之前添加$('.notifications_show').empty() ,以清除目标div中的当前结果。

Just use .append instead of .html: 只需使用.append而不是.html即可:

$.each(enquiryDet, function (i, item) {
      $('.notifications_show').append(enquiryDet[i].name);
});

change this : 改变这个:

$.each(enquiryDet, function (i, item) {
  $('.notifications_show').html(enquiryDet[i].name);
});

to this: 对此:

 $('.notifications_show').empty();//flush content before adding new content
  $.each(enquiryDet, function (i, item) {
   $('.notifications_show').append(enquiryDet[i].name);
 });

html replace the content of selected element and append appends(at last) in the selected element. html替换所选元素的内容,并在所选元素中append (最后)。

Use a variable and append the text to it then append it to the .notifications_show div 使用变量并将文本附加到变量,然后将其附加到.notifications_show div

var text = '';
 $.each(enquiryDet, function (i, item) {
                       text+=enquiryDet[i].name;
                    });
$('.notifications_show').html(text);

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

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