如何将列表与新的api调用结果交换?

时间:2018-07-12 16:31:14

标签: javascript jquery api listview

如何将列表与新的api调用结果交换? 我试图调用clearList函数,但实际上并没有用:( 每次调用API数据时,我在列表中的位置越来越多,我需要清除它们并放一个新的位置。

let arr = [];
// function clearList (){
//     Array.prototype.forEach.call(arr, (el)=> { el.remove(); });
// }
function searching() {
    console.log(arr);
    let search_button = $('#search_button');
    console.log(search_button);
    let sale = $('#sales');
    let ul = $('.show');

    search_button.on('click', function () {

        let findLocation = $('.location').val();
        console.log(findLocation);
        console.log(display_default);
        $.get('api/properties?location=' + findLocation, function (response) {
            let dataLocation = response.result.properties.elements;

            display_default.css('display', 'none');
                for (let key in dataLocation) {
                    arr.push(dataLocation[key].display_address);
                }
                console.log(arr);
                for (let i = 0; i < arr.length; i++) {
                    let li = $('<li>').text(arr[i]);
                    ul.append(li);
                }
        });
    });
}
searching();

});

1 个答案:

答案 0 :(得分:1)

也许可以帮助您?我在附加.empty()

之前先清理ul
 $.get('api/properties?location=' + findLocation)
     .done(function(){
            let dataLocation = response.result.properties.elements;

        display_default.css('display', 'none');
            for (let key in dataLocation) {
                arr.push(dataLocation[key].display_address);
            }
            console.log(arr);

              ul.empty();
            for (let i = 0; i < arr.length; i++) {
                let li = $('<li>').text(arr[i]);
                ul.append(li);
            }
       });