-1

我不知道for循环的正确条件应该是什么,该循环遍历一个数组,该数组的长度取决于我从中提取并添加到数组中的表中有多少行。该数组当前有三行,但 for 循环只打印出两行。我的代码:

//my database query 

query.find({
  success: function(results) {

//this for loop works fine

    for (i = 0; i < results.length; i++){

        eventID = results[i].id;
       activity = results[i].get("activity");
        scene = results[i].get("location");
        neighborhood = results[i].get("neighborhood");
        date = results[i].get("date");
        details = results[i].get("details");
        time = results[i].get("time");
        objIDs.push(eventID);

//each row gets pushed into an array

       search.push([activity, scene, neighborhood, date, details, time]);


        //I empty a div on a page that uses the ajax load() method to load an html page.I replace that html with the array of query results. 

             $('#div1').empty();

       //there are currently 3 rows in my array, but when I loop through it and append() the </br>rows to the div, only one gets printed. I've tried changing the comparison operator to </br>different things but nothing works. I'm definitely getting all the rows from the query because when I alert() the search array I see all the rows. 

       for (i = 0; i <= search.length; i++) {

            $('#mainDiv').append("<div id='event'>" + search[i].join(' ') + "<a href='#' class='interested'>Interested?</a></div>");
        } 

    };// closes for
    },//closes success

  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
}); //closes find 
4

2 回答 2

2

虽然以下是一个问题,它可能不是问题。但是,由于我无法删除此帖子(我必须登录或其他什么?)它仍然是一个工件。请注意它,即使问题是其他问题。


该代码在嵌套i循环中使用相同的变量- 并在两个循环中递增。i

使用不同的变量。

于 2013-05-05T00:03:22.437 回答
2

当您for遍历for它时,您的搜索尚未完全初始化。

};// closes for

    for (i = 0; i <= search.length; i++) {

        $('#mainDiv').append("<div id='event'>" + search[i].join(' ') + "<a href='#' class='interested'>Interested?</a></div>");
    } 

},//closes success
于 2013-05-05T00:06:00.383 回答