javascript - 如何设置喜欢的用户搜索输入?JS
问题描述
我正在开发我的天气网络应用程序。当用户在搜索栏中输入他的城市时,应用程序会显示 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>
以及用于连接 API 的 JS:
$('#search-city-button').on('click', () => {
const searchString = $('#serach-city-input').val();
weatherPage.render(searchString);
weatherPage.renderThreeHours(searchString);
});
解决方案
单击按钮保存时,您应该使用localStorage.setItem('favoriteCity', searchString)
保存用户的值。
然后,当您加载页面时,使用localStorage.getItem('favoriteCity')
检索保存的值。您应该有一个方法 Init() 或使用您的方法render(localStorage.getItem('favoriteCity'))
推荐阅读
- windows - 如何引发 WerFault.exe
- algorithm - 找到组成多边形的矩形联合的轮廓
- javascript - 使用 Hooks 将类组件转换为功能组件
- amazon-s3 - AWS CloudFront 和失效延迟导致重复按需调整图像大小
- javascript - 提供具有列出字段的类型
- python - 通过python-socketio发送数据但无法在flask socketio上接收数据
- windows - 当我“另存为”时,如何在微过滤器中获取引用的原始文件名?
- ios - 在 iOS 中使用动画更改标签背景颜色
- selenium - 如何使用 Selenium::Remote::Driver 抑制“警告”?
- c - 在 C 中每行右侧的每第 n 个位置添加一个逗号