2015-06-28 25 views
0

请原谅,如果这看起来很基本,我已经坚持了几个星期。 如果您使用的是AJAX成功功能HTML网页上动态显示数据,你都可以从服务器返回的数据是stdClass的对象,像这样如何在AJAX函数中通过属性组织对象

array( 
     [7] => stdClass Object 
     (
     [id] => 3 
     [title] => Electrition 
     [img] => 
     [description] => 
     [school] => 
     [location] => 1 
     [url] => 
     [tablename] => 3 
     [votes] => 0 
     [name] => John Doe 
     [NumJobsdone] => 4 
      ) 

     [8] => stdClass Object 
     (
     [id] => 2 
     [title] => Electrition 
     [img] => 
     [description] => 
     [school] => 
     [location] => 1 
     [url] => 
     [tablename] => 2 
     [votes] => 0 
     [name] => Tico Marinez 
     [NumJobsdone] => 6 
     ) 

     [9] => stdClass Object 
     (
     [id] => 2 
     [title] => Engineer 
     [img] => 
     [description] => 
     [school] => 
     [location] => 1 
     [tablename] => 2 
     [votes] => 0 
     [name] => Jerry Smity 
     [NumJobsdone] => 6 
     ) 

    [10] => stdClass Object 
     (
     [id] => 2 
     [title] => Engineer 
     [img] => 
     [description] => 
     [school] => 
     [location] => 1 
     [url] => 
     [tablename] => 2 
     [votes] => 0 
     [name] => Laura Bastian 
     [NumJobsdone] => 6 
     ) 
     ) 

你怎么写一个jQuery功能,可以遍历数据并创建段落标签以标记输出和相应对象的div内的无序列表?

ex。你正试图创建两个分组,电子和工程师,并列出个人的名字。

看起来很简单,但是当我在成功函数中使用$ .each方法时,我为每个具有属性的组织对象的对象创建了段落标记,并且当我尝试使用排序方法时,我一次只能比较两个或三个对象,而不是遍历多个对象的长列表。

说出所需的输出看起来是这样的

 <div id="accordion"> 
     <p> Electrition</p> 
     <div> 
     <ul> 
      <li>jon</li> 
      <li>jill</li>   
     </ul> 
     </div> 

     <p>Engineer</p> 
     <div> 
     <ul> 
      <li>jerry</li> 
      <li>laura</li> 
     </ul> 
     </div> 
     </div> 
+0

首先需要思考它。解决方案非常简单,只需将它们保存在一个数组中,然后循环,检查和打印。 –

+0

除了描述你试过的东西外,还显示你已经尝试过的东西 – vol7ron

回答

相关问题