1 回答

TA貢獻1840條經驗 獲得超5個贊
問題是我設置了一個按鈕,該按鈕的功能是重新加載頁面。我的教授告訴我的解決方案是將整個 JS 代碼設置為一個函數。當按下按鈕時激活該功能。我在這里留下工作代碼:
HTML
<form class="search">
? ? ? ? ? ? <input type="text" id="Search" name="Search" placeholder="Search city..." style="width: 50%; padding: 10px;">? ?
? ? ? ? ? ? <button type="button" id="button" style="padding: 10px;" onclick="find_weather()" >SEARCH</button>
? ? ? ? ?
</form>
JS
let find_weather= function(){
? ? ? ? let location1 = document.getElementById('Search').value;
? ? ? ? let api1 = 'http://api.openweathermap.org/data/2.5/weather?q=';
? ? ? ? let api2 = '&appid=ece23eaa3a8d940b327a0cdc41c1e344&units=metric';
? ??
? ? ? ? let api3 = 'http://api.openweathermap.org/data/2.5/forecast?q=';
? ? ? ? let api4 = '&appid=ece23eaa3a8d940b327a0cdc41c1e344&units=Metric';
? ? ? ??
? ? ? ? let API = api1 + location1 + api2;
? ? ? ? let API2 = api3 + location1 + api4;
? ? ? ??
? ? ? ? fetch(API).then(response => response.json())
? ? ? ? ...
添加回答
舉報