简体   繁体   English

将多个值保存到 LocalStorage 中的同一类型,然后检索这些值

[英]Save multiple values to same type in LocalStorage then retrieve the values

My main requirement is to generate a URL by clicking on the relevant button.我的主要要求是通过单击相关按钮生成 URL。 For example If I click on the green and red button then it should append &_colour=green%2Cred in the url and similar for Gender's button after saving and getting values from localStorage.例如,如果我单击绿色和红色按钮,那么它应该在 url 中的 append &_colour=green%2Cred 和类似的性别按钮在保存并从 localStorage 获取值后。

example.com/product-category/glasses/ ?_gender=women%2Cmen&_colour=green%2Cred example.com/product-category/glasses/ ?_gender=women%2Cmen&_colour=green%2Cred

<div class="main-section">
    <div class="section">
        <button data-gender="men">Men</button>
        <button data-gender="women">Women</button>
    </div>
    <div class="section">
        <button data-colour="green">green</button>
        <button data-colour="red">Red</button>
    </div>
</div>
jQuery('.section button').click(function(e){
    e.preventDefault();
    var gender = jQuery(this).data("gender");
    var colour = jQuery(this).data("colour");
    localStorage.setItem("gender", gender);
    var x = localStorage.getItem("gender");
    console.log(x);
});

The question is how to save multiple values against the same type and retrieve the values to create the url.问题是如何针对同一类型保存多个值并检索这些值以创建 url。

Thanks in Advance.提前致谢。

To retrieve multiple values you need to loop through all the buttons in the page and find the ones which have been clicked on.要检索多个值,您需要遍历页面中的所有按钮并找到已单击的按钮。 You can toggle a class on the buttons when they are clicked in order to identify them more easily.您可以在单击按钮时切换 class,以便更轻松地识别它们。

From there you can use a loop to build an object from the selected values, adding a new data-category attribute to the .section elements to provide the keys in that object.从那里,您可以使用循环从选定的值构建 object,向.section元素添加新的data-category属性以提供 object 中的键。

Finally you can use a URLSearchParams object to build the updated querystring from that object before applying that to history.pushState() to update the current URL.最后,您可以使用URLSearchParams object 从 object 构建更新的查询字符串,然后再将其应用于history.pushState()以更新当前的 URL。

Put all that together and you get this:把所有这些放在一起,你会得到这个:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-section">
  <div class="section" data-category="gender">
    <button data-value="men">Men</button>
    <button data-value="women">Women</button>
  </div>
  <div class="section" data-category="colour">
    <button data-value="green">green</button>
    <button data-value="red">Red</button>
  </div>
</div>
jQuery($ => {
  let $buttons = $('.section button').click(e => {
    e.preventDefault();
    $(e.target).toggleClass('active');
    let data = buildButtonDataObject();
    let querystring = new URLSearchParams(data).toString();

    localStorage.setItem('data', data);
    console.log(data);
    history.pushState(data, 'Page title here', querystring);
    console.log(querystring);
  });
});

let buildButtonDataObject = () => {
  let obj = {};
  $('.section:has(.active)').each((_, s) => obj[s.dataset.category] = $(s).find('button.active').map((_, b) => b.dataset.value).get());
  return obj;
}

Working example工作示例

In the jsFiddle example, you can see the querystring being updated as you click the buttons by opening the 'Network' panel in dev tools.在 jsFiddle 示例中,通过在开发工具中打开“网络”面板,您可以在单击按钮时看到查询字符串正在更新。

You want to make 1 click function and apply for all button in that and assign to local storage?您想单击 function 并申请其中的所有按钮并分配给本地存储吗?

  $('.section button').each(function () {
    var $this = $(this);
    $this.on("click", function () {
      var gender = $this.data("gender");
      var colour = $this.data("colour");
      if (gender != undefined) {
        localStorage.setItem("gender", gender);
      }
      if (colour != undefined) {
        localStorage.setItem("colour", colour);
      }
    });
  });

I add click function to all element in that class ".section button" and check the data and assign in local storage.我将单击 function 添加到该 class“.section 按钮”中的所有元素并检查数据并在本地存储中分配。

Easy way just make onclick to button and pass parameter to function and assign it into localstorage.简单的方法只需将 onclick 设置为按钮并将参数传递给 function 并将其分配到本地存储。

hope this can help u.希望这可以帮助你。

sorry for bad English.抱歉英语不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM