【问题标题】:How to save the checkbox state even after the page refresh?即使在页面刷新后如何保存复选框状态?
【发布时间】:2015-10-14 14:29:29
【问题描述】:

我的 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);
}

【问题讨论】:

    标签: javascript jquery cookies checkbox


    【解决方案1】:

    试试这个方法:

    // 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);
          }
        });
      });
    });
    

    【讨论】:

    • 我确实尝试过这个但没有 $(document).ready(function() 因为我将整个代码放在一个 javascript 文件中并且它没有工作。使用 AJS.log(AJS.Cookie.save (name, true)); 给出未定义。
    • @user_dev 你应该使用$(document).ready。我已经更新了代码 sn-p 中的 cmets。
    • 感谢 Valetin,我正在用我编写的完整代码更新问题中的代码。
    • @user_dev 如果您想在重新加载页面后保持复选框状态,您需要经过以下流程: 1. 等待页面完全加载(例如$(document).ready); 2. 遍历页面上的所有复选框; 3.检查复选框名称是否先前保存到cookie; 4.如果第3步是true,则设置复选框状态为checked; 5.将onchange处理程序附加到复选框; 6.在onchange处理程序内部根据复选框状态设置或删除cookie; 7. 重新加载页面。
    • 我无法投票,因为就积分而言,我处于贫困线以下。需要 15 人投票 :)
    【解决方案2】:

    使用视图状态概念而不是使用其他 cookie 逻辑

    【讨论】:

      【解决方案3】:

      把你所有的逻辑都包进去

      AJS.toInit(function ($) {
          // You can use $ instead of AJS.$ here
          ...
      });
      

      这是 Atlassian 的 $(document).ready(...) 等效项,它允许在调用您的代码之前加载所有 Atlassian 代码。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2020-09-16
      • 1970-01-01
      • 2021-06-07
      • 2021-02-07
      • 2011-03-11
      • 1970-01-01
      相关资源
      最近更新 更多