简体   繁体   中英

jquery to append a table to div

i have a div id .i have to append a table with values to the div.

<div id="testResult" style="padding-left: 120px; "></div>

am doing follwing steps but it is not working.

$.ajax({
    url: '/getReports',
    cache: false
}).done(function (html) {
    if (html != "") {
        $('#testResult').show();
        var htm = "";

        var string1 = '<table border="1"><tr><td>No</td><td>Testcase</td> <td>OS</td> <td>Browser</td>  <td>Result</td></tr></table>';
        $.each(html, function (i, data) {

            string1 + = '<tr><td rowspan="3">1</td><td rowspan="3">' + data.status.test + '</td><td rowspan="3"><!--OS--></td><td>' + data.status.bwser + '</td> <td> ' + data.status.report + ' </td></tr>';
        });
        $('#testResult ').append(string1);
        $("#testResult").html("<br/><br/>");
        $("#testResult").html("<p>" + htm + "</p>");
    }

}).fail(function () {
    $("#testResult").html("Failed to run the test");
    $('#edit ').removeAttr("disabled");
});

$("#testResult").html("<br/><br/>") and $("#testResult").html("<p>" + htm + "</p>") will overwrite contents of your "testResult" DIV. So, replace this

$('#testResult ').append(string1);
      $("#testResult").html("<br/><br/>");
      $("#testResult").html("<p>" + htm + "</p>");   
}

with

$('#testResult ').append(string1);
      $("#testResult").append("<br/><br/>");
      $("#testResult").append("<p>" + htm + "</p>");   
}

.append() : Will add or append extra strings to the existing contents of the DIV where as

.html() : Will removes or overwrite the existing contents of the DIV with newer one.

This is the main difference between .append() and .html() .

edit this part

   $("#testResult").append("<br/><br/>");
  Remove this part-->
  $("#testResult").html("<p>" + htm + "</p>");   

htm is empty string, as you never concatenate any string to it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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