我正在使用 jQuery mobile 并有一组 8 个选择翻转开关,带有开和关值。我需要做的是localStorage
在用户更改时为每个开关设置值。反过来,我还需要localStorage
在页面加载时获取存储的值并相应地设置每个开关。这是我到目前为止所拥有的。它远未完成!
$(document).bind('pageinit', function() {
function setCats(){
var cats = $('#categories select');
var dealCats = localStorage.getItem("Deal Categories");
}
function changeCats (){
var cats = $('#categories select');
for (var i = 0; i < cats.length; i++) {
var obj = cats[i];
}
$('#categories select').on('change', function(){
localStorage.setItem('Deal Categories', JSON.stringify(obj.id));
});
}
$('#categories').on('pagebeforeshow', function(){
setCats();
});
$('#categories').on('pageshow', function(){
changeCats();
});
});
<div data-role="page" id="categories">
<div data-role="header" data-position="fixed">
<a data-rel="back" data-transition="slide" data-mini="true" data-inline="true">Back</a>
<h1>Categories</h1>
</div><!-- header -->
<div data-role="content">
<ul data-role="listview">
<li>
<div data-role="fieldcontain">
<label for="food">Food & Drink</label>
<select name="food" id="food" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="activity">Activity</label>
<select name="activity" id="activity" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="spa">Spa & Salon</label>
<select name="spa" id="spa" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="fitness">Fitness & Health</label>
<select name="fitness" id="fitness" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="homeauto">Home & Auto</label>
<select name="homeauto" id="homeauto" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="medical">Medical/Dental</label>
<select name="medical" id="medical" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="sports">Sports/Leisure</label>
<select name="sports" id="sports" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
<li>
<div data-role="fieldcontain">
<label for="products">Products</label>
<select name="products" id="products" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
</li>
</ul>
</div><!-- content -->
</div><!-- categories -->
有人可以帮忙吗?提前致谢!