在下拉框选择不同value时。ajax向后台请求数据,后台返回数据更新前端页面的数据,此时要求下拉框内容为当前选中值。
一开始在下拉框选择后,页面重新加载后,下拉框值总是重新更新。
具体实现代码贴下:
$(function() { //设置下拉框选择后,页面刷新保持下拉框内容不变
if (localStorage.getItem('parkLot')) {
$("#parkLot option").eq(localStorage.getItem('parkLot')).prop('selected', true);
}
$("#parkLot").on('change', function() {
localStorage.setItem('parkLot', $('option:selected', this).index());
});
});
其实就是在浏览器的存储中取上次选中的下拉框值。
ok。成功。