Javascript 如何设置收藏夹用户搜索输入?JS

Javascript 如何设置收藏夹用户搜索输入?JS,javascript,cookies,Javascript,Cookies,我正在开发我的天气web应用程序。当用户在搜索栏中键入他的城市时,应用程序将显示5天预测。需要设置按钮以在浏览器存储或cookie中保存城市。当用户刷新网页时,自动加载他最喜欢的城市 这是我的html: <div id="search"> <input type="text" id="serach-city-input"> <a id="search-city-button" class="btn btn-danger">Search</

我正在开发我的天气web应用程序。当用户在搜索栏中键入他的城市时,应用程序将显示5天预测。需要设置按钮以在浏览器存储或cookie中保存城市。当用户刷新网页时,自动加载他最喜欢的城市

这是我的html:

<div id="search">
    <input type="text" id="serach-city-input">
    <a id="search-city-button" class="btn btn-danger">Search</a>
</div>

单击保存按钮时,应使用
localStorage.setItem('favoriteCity',searchString)
保存用户的值
然后在加载页面时,使用
localStorage.getItem('favoriteCity')
检索保存的值。您应该有一个Init()方法或使用
呈现(localStorage.getItem('favoriteCity'))的东西。
$(“#搜索城市按钮”)。在('单击',()=>{
const searchString=$(“#serach城市输入”).val();
setItem('serach-city-input',searchString);
weatherPage.render(searchString);
weatherPage.renderThreeHours(搜索字符串);
});
函数fillvaluefromstorage(){
const-storagedValue=localStorage.getItem('serach-city-input');
if(存储值){
$('serach city input').val(存储值);
$(“#搜索城市按钮”)。单击()
}
}
fillValueFromStorege()

搜索

您试过什么吗?您提供的代码提供了上下文,但甚至没有尝试执行您要求的操作。我创建了新按钮以将其添加到收藏夹中,但在本例中,我得到了未捕获的TypeError:localStorage.geItem不是位于app.js:fillValueFromStorege(app.js:26)的函数33@MartinHristov请原谅localStorage.getItem(不是localStorage.geItem)-这是语法错误(错过了t)谢谢,我是盲人:)这次我有错误:未捕获引用错误:未定义搜索字符串。这很奇怪,因为在测试的时候,它会为收藏夹设置一个城市,当刷新时,页面会加载它。但在搜索其他城市时无法更改。已修复,searchString在搜索功能的范围内。在$('favorite button')中重新使用它。在('click',()=>{上,它可以正常工作。谢谢。
$('#search-city-button').on('click', () => {
    const searchString = $('#serach-city-input').val();
    weatherPage.render(searchString);
    weatherPage.renderThreeHours(searchString);
});