我从 jQuery mobile 和 Phonegap 开始。
我有一个类别选择器,并根据所选类别显示属于该类别的复选框列表。请参阅下面我可能会显示的复选框组的 1 个示例。请注意,它是隐藏的,如果用户在“类别”字段中选择“位置”,我只会显示该字段。
<select name="select-choice-0" id="category" onchange="javascript:showboxes();updateresults();">
<option value="0">Select a category</option>
<option value="4">Catering</option>
<option value="1">Locations</option>
<option value="2">Music</option>
</select>
<div id="locationthemes_box" data-role="collapsible" style="display:none;">
<h3>Themes</h3>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Theme</legend>
<input type="checkbox" name="locationthemes" id="checkbox-1" value="2" class="custom" />
<label for="checkbox-1">Castle</label>
<input type="checkbox" name="locationthemes" id="checkbox-2" value="3" class="custom" />
<label for="checkbox-2">Barn</label>
<input type="checkbox" name="locationthemes" id="checkbox-3" value="5" class="custom" />
<label for="checkbox-3">Restaurant</label>
<input type="checkbox" name="locationthemes" id="checkbox-4" value="8" class="custom" />
<label for="checkbox-4">Bar</label>
</fieldset>
</div>
</div>
现在,我有更多想要显示或隐藏的复选框组,具体取决于所选类别。我想防止我拥有页面中未显示的复选框的 HTML。因为这会导致不必要的带宽使用。然后我想:也许我应该通过在用户更改类别选择框后立即对相关复选框和值发出 Web 服务请求来动态加载复选框组的内容。
但这会导致大量的网络请求,我试图节省带宽。
所以我的想法是本地存储可能能够解决我的问题。然后我会将复选框值存储为名称/值字符串:
locationthemes = 2;Castle;3;Barn;5;Restaurant;8;Bar
我的问题:这是要走的路还是有我不知道的最佳实践?
如果我的方式是建议的方式:欢迎使用 PhoneGap 使用本地存储的任何示例!