У меня есть поле ввода с id = "to", которое пустое и когда пользователь вводит что-то, и нажмите кнопку submit, он должен изменить атрибут местоположения в приведенном ниже скрипте. Но ничего не происходит.Получите входное значение и используйте его с переменной с JavaScript
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>
Загружается ли страница после отправки? –
Да, перезагрузка страницы. – mpavlovic89
Почему, по вашему мнению, это должно изменить местоположение? Функция не вызывается после 'onchange'. Вы просто установили новую «oldvalue». – Michelangelo