如何在yahoo weather中将变量分配到javascript src脚本中
下面的代码工作正常,但我试图让它从html表单接收输入,以从表单中选择任何位置。请参阅:text='chicago,il'我希望它接收变量text='location'如何在yahoo weather中将变量分配到javascript src脚本中,javascript,variables,src,assign,Javascript,Variables,Src,Assign,下面的代码工作正常,但我试图让它从html表单接收输入,以从表单中选择任何位置。请参阅:text='chicago,il'我希望它接收变量text='location' <script> var callbackFunction = function(data) { var wind = data.query.results.channel.wind; alert(wind.chill); }; </script> <script src=
<script>
var callbackFunction = function(data) {
var wind = data.query.results.channel.wind;
alert(wind.chill);
};
</script>
<script src="https://query.yahooapis.com/v1/public/yql?q=select wind from weather.forecast where woeid in (select woeid from geo.places(1) where text='chicago, il')&format=json&callback=callbackFunction"></script>
我的一些尝试:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script>
function getLocation() {
var city = document.getElementById("city").value;
var country = document.getElementById("country").value;
var location = city + "," + country;
//alert(location);
}
var callbackFunction = function(data) {
var wind = data.query.results.channel.wind;
document.write(wind.chill)
alert(wind.chill);
};
</script>
<script src="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='location')&format=json&callback=callbackFunction"></script>
</head>
<body>
<input type="text" id="city">
<input type="text" id="country"
<button onclick="getLocation()">Get Weather</button>
</body>
</html>
我也尝试过其他方法,但即使我直接为var location分配一个字符串,比如location=new york,ny或“new york,ny\”等,它也不会返回值。请提供帮助,谢谢。基本上,从表单中获取值后,您需要加载YahooAPI脚本。现在,脚本将在页面加载时加载,这将不起作用。幸运的是,您可以使用javascript向页面添加脚本元素
function getLocation() {
var city = document.getElementById("city").value;
var country = document.getElementById("country").value;
var location = city + "," + country;
//create a script and add it to your page
var script = document.createElement('script');
script.src = "https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='" + location + "')&format=json&callback=callbackFunction";
document.getElementsByTagName('head')[0].appendChild(script);
}
您的警报是否显示输入中输入的位置?我添加了脚本,太棒了,谢谢,但出于某种原因,它保留了返回的值,例如“wind chill”55。即使重新加载页面,该值也保持不变。我会继续努力,但如果你知道一些避免这种情况的方法,请张贴。谢谢Brian停下来,一切都好,我正在进行我之前的一次测试,位置是硬编码的,现在运行良好。非常感谢,不客气。如果可以,请投票/接受我的答案。