我有一个包含数据的JSON文件和一个jquery脚本,该脚本想将这些数据放在列表中,但是由于某种原因,什么也没读取。

这是代码

<div data-role="mainPage" data-theme="a">

<div data-role="header">
    <h1>My Title</h1>
</div>

<div data-role="content">
 <ul data-role="listview" id="fixturesList">

    </ul>
 </div>
</div>

<script type="text/javascript">



$.getJSON("http://localhost/tutorials/results.json", function(matchTable){
//Start off with an empty list every time to get the latest from server
 $('#fixturesList').empty();

 //add the games to be played as a list
 $.each(matchTable, function(i, match){
  ('#fixturesList').append(generateMatchLink(match));
 });

 $('#fixturesList').listview('refresh');

});


function generateMatchLink(match){

    //debugger
    return '<li><a href="javascript:void(0)'
    + '" onclick="goToMovieDetailPage(\''
    + match.Home +'\')">'
    + match.Home
    + '</a></li>';
}

function goToMatchDetailPage(matchHome){

    //create the html template
    var matchPage = $("<div data-role='page' data-url=dummyUrl><div data-role='header'><h1>"
              + matchHome + "</h1></div><div data-role='content'><img border='0' src='
              http://www.songho.ca/opengl/files/gl_mvc01.png' width=204 height=288></img>    </div><div data-role='footer'><h4>"
              + matchHome + "</h4></div></div>");

    //append the new page to the page contanier
    matchPage.appendTo( $.mobile.pageContainer);

    //go to the newly created page
    $.mobile.changePage(matchPage);
}

</script>


这是JSON文件

[{"Fix":"1","Home":"Manchester United","Away":"Aston Villa","Stadium":"Old Trafford"},{"Fix":"2","Home":"Crystal Palace","Away":"Chelsea","Stadium":"Selhurst Park "},{"Fix":"3","Home":"Southampton","Away":"Newcastle United","Stadium":"The Friends Provident St Marys Stadium"},{"Fix":"4","Home":"Stoke City","Away":"Hull City","Stadium":"Britannia Stadium"},{"Fix":"5","Home":"Swansea City","Away":"Norwich City","Stadium":"Liberty Stadium"},{"Fix":"6","Home":"West Bromwich Albion","Away":"Cardiff City","Stadium":"The Hawthorns"},{"Fix":"7","Home":"Arsenal","Away":"Manchester City","Stadium":"Emirates Stadium"},{"Fix":"8","Home":"Fulham","Away":"Everton","Stadium":"Craven Cottage"},{"Fix":"9","Home":"Liverpool","Away":"Tottenham Hotspur","Stadium":"Anfield"}]


有我遗漏的东西吗?

最佳答案

('#fixturesList').append(generateMatchLink(match));


应该

$('#fixturesList').append(generateMatchLink(match));


还有第二个问题,可能是由于复制/粘贴:

var matchPage = $("<div data-role='page' data-url=dummyUrl><div data-role='header'><h1>"
          + matchHome + "</h1></div><div data-role='content'><img border='0' src='
          http://www.songho.ca/opengl/files/gl_mvc01.png' width=204 height=288></img>    </div><div data-role='footer'><h4>"
          + matchHome + "</h4></div></div>");


Javascript字符串不能包含换行符。这样修复:

var matchPage = $("<div data-role='page' data-url=dummyUrl><div data-role='header'><h1>"
          + matchHome + "</h1></div><div data-role='content'><img border='0' src='http://www.songho.ca/opengl/files/gl_mvc01.png' width=204 height=288></img>    </div><div data-role='footer'><h4>"
          + matchHome + "</h4></div></div>");

关于javascript - JSON文件似乎无法被Jquery读取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22718376/

10-11 01:31
查看更多