亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使對象屬性的值可從輸入標簽中搜索

如何使對象屬性的值可從輸入標簽中搜索

泛舟湖上清波郎朗 2022-07-21 10:54:00
我有這個我正在使用的api,我想讓國家(即value.slug == china)成為用戶的搜索項,因為我在代碼中手動輸入它們以獲得每個國家的結果。我希望能夠將 api 與我的 html 表單輸入標簽連接起來,以便用戶自己搜索國家/地區。這是代碼fetch('https://api.covid19api.com/summary')        .then(function (response) {            return response.json();        })        .then(function (data) {            let country = data.Countries.filter((value) => value.Slug == 'china')            appendData(country);        })        .catch(function (err) {            console.log(err);        });
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

使用純 javascript,您可以執行以下操作:


var input = document.querySelector("#input")

input.addEventListener("change", function(event) { // enter key triggers event

   var value = event.target.value;

   console.log(value);

   getData(value);

});


function appendData(data) {

  // stub

  console.log(data);

}


function getData(country) {

fetch('https://api.covid19api.com/summary')

        .then(function (response) {

            return response.json();

        })

        .then(function (data) {

            let country = data.Countries.filter((value) => value.Slug == country)

            appendData(country);


        })

        .catch(function (err) {

            console.log(err);

        });

}

<input type="text" id="input" />


查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號