2017-06-30 51 views
0

我目前正在使用天气站点。我想将用户输入变为改变当前城市的变量。希望你的某个人能帮助我。打提交按钮后使用用户输入更改Javascript变量的值

var userInput = "London"; 
 

 
function changeValue() { 
 
    userInput = document.getElementById("user_Input").value; 
 
    document.getElementById("location").innerHTML = document.getElementById("user_Input").value; 
 
} 
 

 
window.onload = function() { 
 

 
var api = "http://api.openweathermap.org/data/2.5/forecast/daily?q="; 
 
var units = "&units=metric"; 
 
var url2 = api + userInput + units + APPID; 
 

 
var xmlhttp = new XMLHttpRequest(); 
 
xmlhttp.onreadystatechange = function() { 
 
    if(this.readyState == 4 && this.status == 200) { 
 
     myObj = JSON.parse(this.responseText); 
 
     
 
     var iconCode = myObj.list[0].weather[0].id; 
 
     var directions = myObj.list[0].deg; 
 

 
     for (var i = 0; i < 7; i++) { 
 
      
 
      document.getElementById("day" + i).innerHTML = Math.round(myObj.list[i].temp.day) + "°"; 
 
      document.getElementById("icon" + i).src = "sl_line/" + iconCode + ".svg"; 
 
      document.getElementById("wota" + i).innerHTML = wochentag[day.getDay()+i+1]; 
 
      document.getElementById("humidity").innerHTML = Math.round(myObj.list[0].humidity); 
 
      document.getElementById("wind").innerHTML = myObj.list[0].speed + " m/s"; 
 
      document.getElementById("direction").innerHTML = directions; 
 
     } 
 
    } 
 
}; 
 

 
xmlhttp.open("GET", url2, true); 
 
xmlhttp.send(); 
 
};
<article> 
 
    <input id="user_Input" type="text" placeholder="Ortsuchen..."> 
 
    <button id="submit" onclick="changeValue()">Submit</button> 
 
    <span id="location">Unknown</span> 
 
</article> 
 
<section> 
 
     <div> 
 
      <div><span id="day0" class="ml:.25r fs:6r">0</span></div> 
 
      <div><span id="wochentage">0</span></div> 
 
     </div> 
 
     <div> 
 
      <img id="icon0"> 
 
     </div> 
 
     <div> 
 
      <span id="wind">0</span><span id="direction"></span><span></span> 
 
     </div> 
 
     <div> 
 
      <span id="humidity">0</span><span>%</span> 
 
     </div> 
 
</section>

位置得到更新。但变量的值不会改变。我已经浏览了stackoverflow和其他网站,但没有找到任何适合我的解决方案。

+1

你是如何验证该变量的值没有变化? – Satpal

+0

其正确工作什么是错误的.. – Bhargav

+0

你的代码的作品。有什么问题? –

回答

1

1st:您的请求将只能在page load上运行。您需要在用户更改userInput .so之后再次运行该功能,并将其作为一项功能并在每次userInput更改时调用它。

var userInput = "London"; 
 

 
function changeValue() { 
 
    userInput = document.getElementById("user_Input").value; 
 
    document.getElementById("location").innerHTML = document.getElementById("user_Input").value; 
 
    
 
    ajax_call(); 
 
} 
 

 
function ajax_call(){ 
 

 
var api = "http://api.openweathermap.org/data/2.5/forecast/daily?q="; 
 
var units = "&units=metric"; 
 
var url2 = api + userInput + units + APPID; 
 

 
var xmlhttp = new XMLHttpRequest(); 
 
xmlhttp.onreadystatechange = function() { 
 
    if(this.readyState == 4 && this.status == 200) { 
 
     myObj = JSON.parse(this.responseText); 
 
     
 
     var iconCode = myObj.list[0].weather[0].id; 
 
     var directions = myObj.list[0].deg; 
 

 
     for (var i = 0; i < 7; i++) { 
 
      
 
      document.getElementById("day" + i).innerHTML = Math.round(myObj.list[i].temp.day) + "°"; 
 
      document.getElementById("icon" + i).src = "sl_line/" + iconCode + ".svg"; 
 
      document.getElementById("wota" + i).innerHTML = wochentag[day.getDay()+i+1]; 
 
      document.getElementById("humidity").innerHTML = Math.round(myObj.list[0].humidity); 
 
      document.getElementById("wind").innerHTML = myObj.list[0].speed + " m/s"; 
 
      document.getElementById("direction").innerHTML = directions; 
 
     } 
 
    } 
 
}; 
 

 
xmlhttp.open("GET", url2, true); 
 
xmlhttp.send(); 
 

 
} 
 

 
window.onload = function() { 
 

 
    ajax_call(); 
 
};
<article> 
 
    <input id="user_Input" type="text" placeholder="Ortsuchen..."> 
 
    <button id="submit" onclick="changeValue()">Submit</button> 
 
    <span id="location">Unknown</span> 
 
</article> 
 
<section> 
 
     <div> 
 
      <div><span id="day0" class="ml:.25r fs:6r">0</span></div> 
 
      <div><span id="wochentage">0</span></div> 
 
     </div> 
 
     <div> 
 
      <img id="icon0"> 
 
     </div> 
 
     <div> 
 
      <span id="wind">0</span><span id="direction"></span><span></span> 
 
     </div> 
 
     <div> 
 
      <span id="humidity">0</span><span>%</span> 
 
     </div> 
 
</section>

+0

作品!非常感谢你 - 我很感谢你的帮助 –

+0

如果我的回答很有用,用绿色勾号标记它对未来的用户参考很有用。@ DeputyDong_ – JYoThI