如何循环$ .post回调?

时间:2012-01-23 21:50:04

标签: jquery

如何循环$.post回调函数?

这是我想循环的代码......

$.post("p.php",{config: config1, test: test1}, function(data)
{

});             

2 个答案:

答案 0 :(得分:1)

您可以使用$.each方法循环遍历一个或多个对象。以下是使用$.each方法进行数组和对象循环的示例。

循环数组

$.each([52, 97], function(index, value) { 
    alert(index + ': ' + value); 
});

循环对象

var map = { 
  'flammable': 'inflammable', 
  'duh': 'no duh' 
}; 
$.each(map, function(key, value) { 
    alert(key + ': ' + value); 
});

正如您在评论中提到的,response包含4个p标记,您需要将它们附加到页面上的li标记中,您可以尝试使用此标记。

$.post("p.php",
    { config: config1, test: test1}, 
    function(data){ 
      //if you have an to li then use id selector or 
      //if you have a class to li then use class selector
      $('liSelector').append(data);
});  

答案 1 :(得分:0)

$.each(data, function(k,v) {

...

});