我对编码非常环保,需要技术问题的帮助。我正在使用Ajax提交一个应该返回10个结果的查询。它就是这样做的,但是当我输入空方法来清除保存结果的div以便提交由事件处理程序触发的新查询时,它只显示一个结果而不是十个结果。
我已经省略$(#returns).empty()以确保我的代码有效并返回10个结果。
url: queryURL,
method: "GET"
}).then(function(response){
console.log(response.data);
var imageResults = response.data;
for (var i=0; i<imageResults.length; i++){
var gifDiv = $("<div id='returns'>");
var rating = imageResults[i].rating;
var ratingP = $("<p>").text("Rating: " + rating);
// console.log(ratingP);
var giph = $("<img>");
var stillGiph = imageResults[i].images.fixed_width_small_still.url;
giph.attr("src", stillGiph); //Does the attribute need two arguments? Can I pass anim/still giphs in if statements separately? Does if statements for movement need to be a separate function?
//empty returns div prior to appending new information
$("#returns").empty();
gifDiv.append(giph, ratingP);
$("#returns").prepend(gifDiv);
};
});
在运行$("#returns").empty();
循环之前,您需要调用for
。就目前而言,您的代码大致相似
fetch 10 details then
for detail in details
empty returns div
add an item
你需要的是什么
fetch 10 details then
empty returns div
for detail in details
add an item