开发者

using Jquery to get XML and put into html table

开发者 https://www.devze.com 2023-04-10 05:38 出处:网络
I know this has been asked before, but I can\'t figure out what\'s wrong, I\'m trying to load in a XML file with a list of shows using jQuery so that the shows can be updated in one file, and upload o

I know this has been asked before, but I can't figure out what's wrong, I'm trying to load in a XML file with a list of shows using jQuery so that the shows can be updated in one file, and upload onto multiple pages. To me it seems like I'm doing everything right, but my knowledge jquery is fragile at best. Most of it is just pieced together from answers to others questions.

my HTML

<aside id="shows"class="aside shadow">
<div class="text" id="table">
<div class="more low"> MORE SHOWS </div>
<div class="less"> LESS SHOWS </div>
</div>
</aside>

my Jquery

function showData() {
$.ajax({
   type: "GET",
   url: "shows.xml",
   dataType: "xml",
   success: function getShows(a) {
    $('#table').append('<h2>SHOWS</h2>'); 
    $('#table').append('<table>'); 

    $(a).find('show').each(function(){
        var $show = $(this);
        var date = $show.find('date').text();
        var place = $show.find('place').te开发者_JAVA技巧xt();
        var location = $show.find('location').text();
        var time = $show.find('time').text();

        var html = '<tr><td class="bold">' + date + '</td><td class="hide">' + place + '</td><td>' + location + '</td><td class="bold">' + time + '</td></tr></table>';

        $('<table>').append(html);

    });
    }
 });
}

and XML

<shows>
<show>
    <date>9/8</date>
    <place>Toads Place</place>
    <location>New Haven, CT</location>
    <time>9PM</time>
</show>

</shows>

This does NOTHING and This looks totally right to me, so I'm super confused. Knowing me, I'm missing a semi colon. ><

Thanks!!


Try to put the exact url...

url: "shows.xml",

...maybe the shows.xml isn't in the same folder than the Jquery ajax function


Can you confirm the ajax call works fine and the success method is called.
You can check the ajax call and the response on firebug.
If the xml is returned fine, you may want to try below, modified the success method a little e.g. http://jsfiddle.net/Jayendra/2PFxr/

Try -

$.ajax({
    type: "GET",
    url: "shows.xml",
    dataType: "xml",
    success: function(xml){
        $('#table').append('<h2>SHOWS</h2>'); 
        $('#table').append('<table id="show_table">'); 
        $(xml).find('show').each(function(){
            var $show = $(this);
            var date = $show.find('date').text();
            var place = $show.find('place').text();
            var location = $show.find('location').text();
            var time = $show.find('time').text();
            var html = '<tr><td class="bold">' + date + '</td><td class="hide">' + place + '</td><td>' + location + '</td><td class="bold">' + time + '</td></tr>';
            $('#show_table').append(html);
        });
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号