【问题标题】:.empty() method changes the number of results return.empty() 方法改变返回结果的数量
【发布时间】:2019-09-09 01:01:06
【问题描述】:

我对编码非常熟悉,并且在技术问题上需要帮助。我正在使用 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);
            };
        });

【问题讨论】:

    标签: javascript jquery ajax append dom-manipulation


    【解决方案1】:

    在运行for 循环之前,您需要调用$("#returns").empty();。就目前而言,您的代码大致类似于

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-11
      • 2023-03-10
      • 2018-03-17
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 2012-11-20
      • 2019-01-20
      相关资源
      最近更新 更多