在jQuery中获取动态数组的价值

时间:2014-03-01 06:41:46

标签: jquery

我想在jQuery中解析嵌套的动态数组。  代码是: -

new Ajax.Request(url, {    // url of the controller
method: 'get',
parameters: { search: searchString }, //parameter 
onComplete: function(transport) {
  alert(transport.responseText);
      var values = transport.responseText;
      alert(values.length);
      // response from the controller will get alerted.
    for(var i=0;i<values.length;i++){
for (var x in values[i]){
   console.log(x+"="+values[i][x]);
  }
 }


}
});

当我提醒数组然后它显示数组是这样的: -

 Array
  (
    [0] => Array
    (
        [post_id] => 1
        [title] => Womens Health
        [post_content] => Lorem Ipsum is simply dummy text of the printing and typesetting industry.
        [status] => 1
        [created_time] => 2014-01-20 20:15:34
        [update_time] => 2014-02-27 18:51:42
        [identifier] => women_health
        [user] => aheadWorks
        [update_user] => Bod christen
        [meta_keywords] => Must-have Magento Extensions, aheadWorks
        [meta_description] => Must-have Magento Extensions
        [comments] => 0
        [tags] => 
        [short_content] => <p><img src="{{skin url='images/journal.jpeg'}}" alt="" /></p>
    )

[1] => Array
    (
        [post_id] => 2
        [title] => Ladies Home Joural
        [post_content] => <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>
        [status] => 1
        [created_time] => 2014-02-17 18:02:54
        [update_time] => 2014-02-27 18:49:46
        [identifier] => ladies_home_joural
        [user] => Bod christen
        [update_user] => Bod christen
        [meta_keywords] => 
        [meta_description] => 
        [comments] => 0
        [tags] => 
        [short_content] => <p><img src="{{skin url='images/journal1.jpeg'}}" alt="" /></p>
    )

 )

2 个答案:

答案 0 :(得分:0)

我不确定我理解你的问题,但这样的事情可能有用:

for(var i=0; i < your_array.length; ++i){
    console.log(your_array[i]['title']);
    console.log(your_array[i]['short_content']);
}

答案 1 :(得分:0)

试试这个:

假设您调用数组values

for(var i=0;i<values.length;i++){
    for (var x in values[i]){
       console.log(x+"="+values[i][x]);
    }
}