简体   繁体   中英

Loading an Array from Java Script into Web page

I have included a fiddle here: jsfiddle.net/LgE3N

I have a series of arrays in a javascript file that I need to import into an web page. I seem to be able to work with the arrays in the script and have been able to output totals of numbers into the page, however, I do not seem to be able to figure out how to print the entire list into the web page.

    window.onload = function ()
    {
$("#contributions").html (totalContributions.toFixed(2));
$("#amount").html (totalContributors);  

var dateString = "";
for (var j = 0; j < date.length; j++)
{
    dateString += date[j];
    $("#dateArray").html (dateString);
}
    }

.

    <div id="data_list">
    <table rules="rows" cellspacing='0'>
        <thead>
            <tr>
                <th>Date</th>    
                <th>Amount</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Address</th>
            </tr>
            <td>
                <p>dateArray</p>
                <id "dateArray">
            </td>
            <td>
                <p>amountArray</p>
            </td>
            <td>

This isn't really hard, you need a loop that returns each row and append it to the table.

var count = firstName.length, sum = 0;

for (var i = 0; i < count; i++) {
    sum += parseFloat(amount[i]);
    html = '<tr><td>'+ date[i] +'</td><td>'+ amount[i] +'</td><td>'+ firstName[i] +'</td><td>'+ lastName[i] +'</td><td><p>'+ street[i] +'<br>'+ city[i] +' '+ state[i] +' '+ zip[i] +'</p></td></tr>';
    $('#data').append(html);
};

$('#contributions').html(count);
$('#amount').html(sum);

DEMO: http://jsfiddle.net/8XvQw/

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