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

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

document.getElementById(' ').value 返回未定義

document.getElementById(' ').value 返回未定義

慕桂英546537 2023-04-01 17:20:47
我正在為天氣應用程序使用 API。但是當涉及到使用 a<input type="text">和 a創建搜索欄時document.getElementById(' ').value,它會返回未定義的(當我注銷時)。所以我想知道是否有任何其他方式來獲取輸入。HTML:<form class="search">     <input type="text" id="Search" name="Search" placeholder="Search city..." style="width: 50%; padding: 10px;">     <button type="submit" id="button" style="padding: 10px;">SEARCH</button></form>腳本:let location1 = document.getElementById('Search').value;let api1 = 'http://api.openweathermap.org/data/2.5/weather?q=';let api2 = '&appid=ece23eaa3a8d940b327a0cdc41c1e344&units=metric';let API = api1 + location1 + api2;// MAIN WEATHER */fetch(API) // Current weather API任何幫助將不勝感激
查看完整描述

1 回答

?
慕斯709654

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())

? ? ? ? ...


查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 593 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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