如何在页面刷新后保存复选框状态
How to save the checkbox state even after the page refresh?
我有多个复选框在我的UI,当检查做一些操作。但是当我刷新页面时,所有的复选框又被选中了。我如何使用AJS。Cookie (Atlassian Javascript框架)来保存状态。源代码,我写的,但它给出的cookie值为未定义。
'#generate-canvas-button'
是传递所有选中复选框的按钮的id。
// Wait until the page is completely loaded.
AJS.$(document).ready(function() {
// Iterating over all checkboxes on page.
$('input:checkbox').each(function() {
// Getting checkbox name.
var name = $(this).attr('name');
// Checking saved cookie.
if (AJS.Cookie.read(name)) {
// Updating checkbox state.
$(this).prop('checked', true);
}
});
// Clicking the OK button should run submit(), pop up displays all checked boxes
$('#generate-canvas-button').click(submit);
});
function submit() {
var checked = [];
var targetGroupActors = [];
var bigPictureActors = [];
var bigPictureImpacts = [];
var productDetailsActors = [];
var productDetailsDeliverable = [];
// Iterating over all checkboxes on page.
$('input:checkbox').each(function() {
// Getting checkbox name.
var name = $(this).attr('name');
// Checking checkbox state.
if ($(this).is(":checked")) {
// Saving checkbox name to cookie.
AJS.Cookie.save(name, true);
} else {
// Remove checkbox state from cookie.
AJS.Cookie.erase(name);
}
if ($(this).is(":checked")) {
impactMapValues = $( this ).prop('id');
impactMapActor = $( this ).prop('name');
var value = document.getElementById(impactMapValues).value;
if (impactMapActor == "actor-checkbox") {
targetGroupActors.push(value);
}
if (impactMapActor == "impact-checkbox") {
var result = value.split(",");
actor_value = result[0];
impact_value = result[1];
bigPictureActors.push(actor_value);
bigPictureImpacts.push(impact_value);
}
if (impactMapActor == "deliverable-checkbox") {
var result = value.split(",");
actor_value = result[0];
deliverable_value = result[1];
productDetailsActors.push(actor_value);
productDetailsDeliverable.push(deliverable_value);
}
checked.push(value);
}
});
addTotargetGroup(targetGroupActors);
addToBigPicture(bigPictureActors,bigPictureImpacts);
addReleaseTarget(productDetailsActors,productDetailsDeliverable);
}
试试这个方法:
// Wait until the page is completely loaded.
$(document).ready(function() {
// Iterating over all checkboxes on page.
$('input:checkbox').each(function() {
// Getting checkbox name.
var name = $(this).attr('name');
// Checking saved cookie.
if (AJS.Cookie.read(name)) {
// Updating checkbox state.
$(this).prop('checked', true);
}
// Attaching onchange handler.
$(this).change(function() {
// Checking checkbox state.
if ($(this).is(":checked")) {
// Saving checkbox name to cookie.
AJS.Cookie.save(name, true);
} else {
// Remove checkbox state from cookie.
AJS.Cookie.erase(name);
}
});
});
});
使用viewstate概念而不是使用其他cookie逻辑
用
AJS.toInit(function ($) {
// You can use $ instead of AJS.$ here
...
});
这是Atlassian的等效$(document).ready(...)
,它允许在调用你的代码之前加载所有Atlassian代码。
相关文章:
- 如何保存动态创建的复选框的更改状态
- 如何通过单击唯一按钮保存下拉菜单/复选框菜单选项和占位符值
- 如何使用javascript自动提交保存/重新填充的复选框
- 如何使用复选框启用选项并通过本地存储进行保存
- JavaScript-使用sessionStorage保存文档之间共享的复选框状态
- 将复选框选中的状态保存并加载到数据库
- 如何保存和访问复选框数据 HTML
- 如何将复选框数组中的数据保存到数据库中
- 如何在 javascript 的信息窗口中使用 cookie 保存复选框状态
- 单击时自动保存复选框
- localStorage如何保存复选框
- 具有Raspberry Pi nginx网页保存复选框状态
- 单击时保存复选框的状态
- 在HTML或Javascript中保存复选框状态
- 如何在页面刷新后保存复选框状态
- 引导弹出窗口不保存复选框
- 正在浏览器缓存中保存复选框值
- 保存复选框的动态状态时遇到问题(Javascript,jQuery)
- 本地存储未保存复选框未选中阶段
- jQuery-在cookie中保存复选框条件