我假设您正在尝试用其他东西替换您的 Ajax 发布请求。
我的建议是使用 cookie 将一些信息保存在本地供 js 使用。
首先,像这样将jQuery & jQuery Cookie 添加到您的网页顶部(在<head> 标签内):
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js.js" charset="utf-8"></script>
然后,在您的 javascript 文件中,执行以下操作:
(function($) {
// Grabs cookie and puts into a js variable.
var toggled = $.cookie('toggled');
// On click of checkbox, check if set/unset. Then set cookie to true/false.
$('#switch').change(function()) {
if($(this).is(':checked')) { // If set to true, set toggled to true.
$.cookie('toggled', true, { expires: 1, path: '/' });
} else { // Else, set to false.
$.cookie('toggled', null, { expires: 1, path: '/' });
}
}
})(jQuery);
然后,您需要在刷新时检查切换的 cookie,并添加行为以在切换为 true 时选中该框:
if(toggled == 'true') { //If cookie set to true...
$('#switch').attr('checked','checked').flipswitch("refresh") // Flip the switch (Important for jquery mobile).
}
有关工作示例,请参阅 jsFiddle。
我希望这会有所帮助。