2013-04-24 95 views
1

当试图动态创建一个动态的ListView我得到这个在jquery mobile中创建动态列表视图不工作?

what i get

这里是我的JSON

{"eventid":["61","23"],"name":["Clare Birthday","Mums Birthday"],"enddate":["Sat 27th April 2013","Wed 19th June 2013"]} 

,这里是我的代码。

<script type="text/javascript"> 
           $(function(){ 
            var items=""; 
             $.getJSON("ajaxResponder.php?method=check-events",function(data){ 
              $("#contacts").html(
              '<li data-role="list-divider" role="heading">Live Events</li>'+ 
               $.each(data,function(index,item){ 
                '<li><a href="check-events-details?eventid='+item.eventid+'" data-transition="slide">'+item.name+ 
                '<p>End Date: '+item.enddate+'</p></li>' 
               })); 
              $("#contacts").listview("refresh"); 
             }); 
           }); 

         </script> 

      <div data-role="fieldcontain"> 
      <ul id="contacts" data-role="listview" data-divider-theme="b" data-inset="true"> 
      </ul> 
      </div> 

任何人都可以看到我哪里去错了吗?

+0

我猜'var items =“”;'应该是'var items = [];' – Omar 2013-04-24 17:57:06

+0

感谢您的回答奥马尔,但没有运气。 – 2013-04-24 18:06:51

+1

您没有正确读取数组。试试'item [index] .eventid' – Omar 2013-04-24 19:04:33

回答

1

问题是,您没有正确读取数组。以下解决方案基于假设您想要创建两个链接到id的链接。有很多种读取数组的方式,这取决于你想如何输出数据。

var object = { 
"eventid": ["61", "23"], 
"name": ["Clare Birthday", "Mums Birthday"], 
"enddate": ["Sat 27th April 2013", "Wed 19th June 2013"] 
}; 

首先,您需要将JSON对象转换为数组。

var array = [object]; 

然后在里面循环。我在这里使用for语句来循环两次。

​​

Demo

注:id应以字母而不是数字开头。