【发布时间】:2016-02-03 19:14:33
【问题描述】:
我有一个 id="to" 为空的输入字段,当用户输入内容并按下提交时,它应该更改下面脚本中的位置属性。但是,什么都没有发生。
oldvalue = document.getElementById("to").value;
var newvalue;
var inputBox = document.getElementById("to");
inputBox.onchange = function(){
newvalue = document.getElementById("to").value;
oldvalue = newvalue;
};
$(document).ready(function() {
$.simpleWeather({
location: oldvalue,
unit: 'c',
success: function(weather) {
html = '<h2><i class="icon-'+weather.code+'"></i> '+weather.temp+'°'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li>';
$("#weather").html(html);
},
error: function(error) {
$("#weather").html('<p>'+error+'</p>');
}
});
});
HTML 输入
<div class="input-group add-on">
<input type="text" class="form-control" placeholder="to: " id="to">
<button id='tobtn' class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
【问题讨论】:
-
提交后页面是否重新加载?
-
为什么你认为它应该改变位置?在
onchange之后不会调用该函数。您只需设置一个新的oldvalue。 -
@mpavlovic89 因此页面重新加载,您的页面重置为初始状态。难怪什么都没发生。
-
我不确定我是否在关注你。我知道有问题,但我看不到解决方案。
-
有没有办法让它这样工作?我的意思是,类似的方式?
标签: javascript jquery input weather-api