如何使用jQuery在localStorage中保存选择选项? [英] How to save select option in localStorage with jQuery?
本文介绍了如何使用jQuery在localStorage中保存选择选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将所选选项保存为HTML格式的localStorage,因此,如果刷新页面,则所选内容保持选中状态.
I am trying to save selected option in localStorage in HTML, so if you refresh page, the selection stays selected.
我已经尝试过:
HTML:
<select id="edit">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
jQuery:
$(function() {
$('#edit').change(function() {
localStorage.setItem('todoData', this.innerHTML);
});
if(localStorage.getItem('todoData')){
localStorage.getItem('todoData');
}
});
但这似乎行不通,任何想法,非常感谢您的帮助...
But this seems not to work, any ideas, realy thanks for help...
此处的实时演示: http://jsfiddle.net/nwk1g6vp/
推荐答案
存储并设置值,而不是选择内容的整个HTML
Store and set the value, not the entire HTML of the select
$(function() {
$('#edit').change(function() {
localStorage.setItem('todoData', this.value);
});
if(localStorage.getItem('todoData')){
$('#edit').val(localStorage.getItem('todoData'));
}
});
这篇关于如何使用jQuery在localStorage中保存选择选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文