如何在页面重新加载后保持复选框检查状态?

刷新页面后,我无法继续检查复选框

您需要使用cookie …一旦用户点击复选框,将其状态存储到cookie中,只需在页面加载时获取cookie值,以预先设置复选框,就像它们在之前的选择中一样。

HTML

获取复选框值并从中创建cookie

 var checkboxValues = {}; $(":checkbox").each(function(){ checkboxValues[this.id] = this.checked; }); $.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' }) 

读取cookie以预先填充负载的function

 function repopulateCheckboxes(){ var checkboxValues = $.cookie('checkboxValues'); if(checkboxValues){ Object.keys(checkboxValues).forEach(function(element) { var checked = checkboxValues[element]; $("#" + element).prop('checked', checked); }); } } 

工作小提琴

你必须在服务器端这样做。 使用PHP,ASP.NET或您使用的任何东西。

这是唯一的方法。

或者 ,如果您的网站只是客户端,那么您必须在没有任何页面刷新的情况下实现它。

我在这里怎么做:

    onclick="getFileFromServer('write_ckfile.php?target=.cktest&value='+this.checked, function(text){ show_write(text)});"> cktest with php
cktest with ajax

和文件write_ckfile.php:

   

我希望这有帮助。 MiKL〜