简体   繁体   English

本地存储多个值

[英]localstorage multiple values

Alright, so i have textbox where I enter grades, and then they're saved in localstorage and i can see them under my textbox. 好吧,所以我有一个文本框,在其中输入成绩,然后将它们保存在本地存储中,我可以在我的文本框下看到它们。 But if I want to add more text boxes in which i can add more grades, and they will be saved in localstorage but they will be in another array or something how i can do that? 但是,如果我想添加更多的文本框,可以在其中添加更多的成绩,它们将保存在本地存储中,但它们将位于另一个数组中,或者我该怎么做? I will be grateful for any help. 我将不胜感激。

$(document).ready(function () {
    var i = 0;
    for (i = 0; i < localStorage.length; i++) {
        var gradeID = "grade-" + i;
        $('#gradeList').append("<b id='" + gradeID + "'>" + localStorage.getItem(gradeID) + "</b>, ");
    }

    $('#clear').click(function () {
        localStorage.clear();
    });

    $('#gradeEntryForm').submit(function () {
        if ($('#gradeInput').val() !== "") {
            var gradeID = "grade-" + i;
            var gradeMessage = $('#gradeInput').val();
            localStorage.setItem(gradeID, gradeMessage);
            $('#gradeList').append("<b class='task' id='" + gradeID + "'>" + gradeMessage + "</b>, ");
            var grade = $('#' + gradeID);
            task.slideDown();
            $('#gradeInput').val("");
            i++;
        }
        return false;
    });

    $('#gradeList').on("click", "b", function (event) {
        self = $(this);
        gradeID = self.attr('id');
        localStorage.removeItem(gradeID);
        self.remove();

    });


});

DEMO 演示

You can set dynamic id to your LocalStorage keys which you can relate to your generated TextBoxes 您可以为您的LocalStorage键设置动态ID,该键可以与生成的TextBoxes相关

Something like below 像下面这样

localStorage['grade_' + some_id] = value;

You could use the following example: 您可以使用以下示例:

document.getElementById("addGrade").onclick = function() { var gradeCell = document.getElementById("gradeCell"); var input = document.createElement("input"); input.type = "number"; var br = document.createElement("br"); gradeCell.appendChild(input); gradeCell.appendChild(br); }

EXAMPLE

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

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