[英]Persisting checked state using localstorage
I have taken the following snippet from a previously asked question on how to store the checked/unchecked status of all checkboxes on a page in localstorage: 我从先前的问题中摘录了以下片段,该片段有关如何在本地存储中存储页面上所有复选框的选中/未选中状态:
$(function(){
var test = localStorage.input === 'true'? true: false;
$('[type="checkbox"]').prop('checked', test || false);
});
$('[type="checkbox"]').on('change', function() {
localStorage.input = $(this).is(':checked');
console.log($(this).is(':checked'));
});
When I select one of the checkboxes and then refresh the page, once it reloads every single checkbox is checked. 当我选择其中一个复选框然后刷新页面时,页面重新加载后,每个复选框都会被选中。
How would I make this store each individual checked state? 我将如何使该存储每个单独的检查状态?
Note I may have between 0 - 50 check boxes available depending on how many outstanding records there are in my gridview so I don't have fixed input id's to use only a record id associated to each row. 注意根据网格视图中有多少未完成的记录,我可能有0到50个复选框可用,因此我没有固定的输入ID仅使用与每一行关联的记录ID。
If you want to rely on a localStorage solution, you may do something like this: 如果要依赖localStorage解决方案,则可以执行以下操作:
$(function(){
$('[type="checkbox"]').each(function () {
var $this = $(this),
name = $this.attr('name');
$this.prop('checked', localStorage[name] === 'true');
});
});
$('[type="checkbox"]').on('change', function() {
var $this = $(this),
name = $this.attr('name');
localStorage[name] = $this.is(':checked');
});
http://jsfiddle.net/mct7xgq2/ http://jsfiddle.net/mct7xgq2/
The first part is executed on page load and sets the checked state depending on the localStorage[name] value, where name is the input's name attribute. 第一部分在页面加载时执行,并根据localStorage [name]值设置检查状态,其中name是输入的name属性。
The second part executes when any checkbox is being changed: it takes the input's name, as before, but instead of reading the value, it writes it. 第二部分在更改任何复选框时执行:它像以前一样使用输入的名称,但是它不是读取值而是写入值。
IF the page would not load, it would be better to just store the values in JS
object rather than using localStorage
. 如果页面无法加载,最好将值存储在
JS
对象中,而不是使用localStorage
。
Just create a JS object and keep pushing values inside it and store 只需创建一个JS对象并继续在其中推送值并存储
Client Side Solution if the page does not reload 如果页面未重新加载,则为客户端解决方案
$(function(){
var checkedItems ={};
$('[type="checkbox"]').on('change', function() {
//Store the ID value also in the localstorage
if($(this).is(':checked')){
var id = $(this).attr('id'); //Get the id if this is checked
checkedItems['id_'+id] = id;
}
});
});
If Page reloads, then your best bet is to use server side concepts like Session
. 如果Page重新加载,那么最好的选择是使用诸如
Session
类的服务器端概念。 Send an AJAX
request each time a particular checkbox is checked and store it in a session. 每次选中特定复选框时,发送
AJAX
请求并将其存储在会话中。
SERVER SIDE SOLUTION 服务器端解决方案
$(function(){
$('[type="checkbox"]').on('change', function() {
//Store the ID value also in the localstorage
if($(this).is(':checked')){
var id = $(this).attr('id'); //Get the id if this is checked
$.ajax({
type: 'POST',
url: your server side url path, //store it in a session
data: {'id':id},
dataType: 'html',
success: function(result){
//so some functionality
}
});
}
});
});
Check out the FIDDLE LINK 查看FIDDLE LINK
This is a solution for d3.js with a ternary operator use. 这是使用三元运算符的d3.js解决方案。 It compares the id of this checkbox with the values stored under key=id in localStorage.
它将此复选框的ID与存储在localStorage中key = id下的值进行比较。 If the value is "true" then 'this.checked' is set to 'true', else to 'null' ( null indicates: no 'checked' property)
如果值为“ true”,则将“ this.checked”设置为“ true”,否则设置为“ null”(null表示:无“ checked”属性)
var box = d3.selectAll(".box").each(function(){
var id = this.id;
var storageValue = localStorage.getItem(id);
this.checked = storageValue === "true" ? true : null;
});
Previously I have setItem in localStorage. 以前我在localStorage中有setItem。 The checkbox are created dynamically appended to rows in a table
复选框是动态创建的,附加到表中的行上
rows.append('input')
.attr('type','checkbox')
which in turn is based on the data from a cvs. 反过来又基于来自cvs的数据。 With the following ids for the checkboxes:
带有以下ID的复选框:
.attr("id", function(d,i) { return 'box'+' '+i; })
And the 'change' of the checkbox state: 复选框状态的“更改”:
d3.selectAll(".box").on("change", function() {
var id = this.id;
if ( this.checked ){
localStorage.setItem(id, this.checked);
}
else {
localStorage.removeItem(id);
}
});
I have thousands of rows, each with a checkbox. 我有数千行,每行都有一个复选框。 I did not see a major issue on the timeline inspection.
我没有在时间表检查中看到主要问题。 So I guess that this is a good solution.
因此,我认为这是一个很好的解决方案。 I am not a d3.js expert.
我不是d3.js专家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.