简体   繁体   English

javascript localStorage添加到表

[英]javascript localStorage add to table

For an assignment I need to use javascript/jquery, but I'm new to it. 对于作业,我需要使用javascript / jquery,但我是新手。 I have a form with some text boxes where I can create a new animal. 我有一个带有一些文本框的表单,可以在其中创建新的动物。 The animal will be added to an array called animal_list. 该动物将被添加到名为animal_list的数组中。

Animal_list will be added to localstorage and it works fine. Animal_list将被添加到localstorage,并且可以正常工作。 But what I want is to load all animals from the local storage and add them to a table. 但是我想要从本地存储中加载所有动物并将它们添加到表中。 The type of animals that will be stored are a cat or a dog and they also have their own table. 将要存储的动物是猫还是狗,它们也有自己的桌子。

I have a console.log where I write the type of the animal, and this shows the right output (3 dogs, 2 cats), but they won't be added to a table. 我有一个console.log,其中写入了动物的类型,这显示了正确的输出(3只狗,2只猫),但是不会将它们添加到表中。

This is my HTML code: 这是我的HTML代码:

<div class="row">
    <div class="col-lg-6">
      <h3>Honden</h3>
      <table id="dog" class="table table-hover table-striped table-condensed">
        <thead>
          <tr>
            <th>CRN</th>
            <th>Name</th>
            <th>DoB</th>
            <th>Gender</th>
            <th>Last Walk Date</th>
          </tr>
        </thead>
      </table>
    </div>
    <div class="col-lg-6">
      <h3>Katten</h3>
      <table id="cat" class="table table-hover table-striped table-condensed">
        <thead>
          <tr>
            <th>CRN</th>
            <th>Name</th>
            <th>DoB</th>
            <th>Gender</th>
            <th>Bad Habits</th>
          </tr>
        </thead>
      </table>
    </div>
  </div>

And this is my javascript/jQuery code: 这是我的javascript / jQuery代码:

$( document ).ready(function(){
    /* Eerst alle animals ophalen */
    var animal_list = JSON.parse(localStorage.getItem("animals"));

    if (Array.isArray(animal_list)) {
      for (var i = 0; i < animal_list.length; ++i)
      {
        /* get a specific animal from our array so we can use it's properties */
        var animal = animal_list[i];

        console.log("Loading next animal...");
        console.log("Animal type: " + animal.Type);

        if(animal.Type == "dog")
        {
          // animal is a dog

          var table = document.getElementById("#dog");

          row = table.insertRow(i+1);
          Crn = row.insertCell(0);
          Name = row.insertCell(1);
          DoB = row.insertCell(2);
          Gender = row.insertCell(3);
          Special = row.insertCell(4);

          Crn.innerHTML(animal.Crn);
          Name.innerHTML(animal.Name);
          DoB.innerHTML(animal.Dob);
          Gender.innerHTML(animal.gender);
          Special.innerHTML(animal.Special);

          save(Crn.innerHTML);
          save(Name.innerHTML);
          save(DoB.innerHTML);
          save(Gender.innerHTML);
          save(Special.innerHTML);



        }
        if(animal.Type == "cat")
        {
          // animal is a cat
          var table = document.getElementById("#cat");

          row = table.insertRow(i+1);
          Crn = row.insertCell(0);
          Name = row.insertCell(1);
          DoB = row.insertCell(2);
          Gender = row.insertCell(3);
          Special = row.insertCell(4);

          Crn.innerHTML(animal.Crn);
          Name.innerHTML(animal.Name);
          DoB.innerHTML(animal.Dob);
          Gender.innerHTML(animal.gender);
          Special.innerHTML(animal.Special);

          save(Crn.innerHTML);
          save(Name.innerHTML);
          save(DoB.innerHTML);
          save(Gender.innerHTML);
          save(Special.innerHTML);

        }
      }
    }
  });

The console in safari gives no error messages. Safari浏览器中的控制台未显示任何错误消息。 I also tried to change 我也尝试改变

document.getElementById("#dog");

to (also for cat) 到(也用于猫)

document.getElementById("dog");

but with no result. 但没有结果。

Could someone help me with my problem? 有人可以帮我解决我的问题吗?

Thanks in advance! 提前致谢!

EDIT: I fount a typo (animal.type should be animal.Type). 编辑:我发现一个错字(animal.type应该是animal.Type)。 When changed that I got an error message in my console: 更改后,我在控制台中收到一条错误消息:

TypeError: null is not an object (evaluating 'table.insertRow') TypeError:null不是对象(正在评估“ table.insertRow”)

(typo fixed in code) (打字错误已在代码中修正)

I used jQuery to add the objects in the localStorage to a table. 我使用jQuery将localStorage中的对象添加到表中。

This is an example for the dog-table 这是狗桌的一个例子

if(animal.Type == "dog")
        {
          // animal is a dog
          var table = document.getElementById("dog");

          $("#dog").append("<tr><td>" + animal.Crn + "</td><td>" + animal.Name + "</td><td>" + animal.Dob +"</td><td>" + animal.Gender + "</td><td>" + animal.Special + "</td></tr>");
        }

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

相关问题 将本地存储项添加到 javascript 数组中 - Add localstorage items into a javascript array 如何使用 javascript 添加 localStorage? - How to add localStorage using javascript? 如何在表上向本地存储添加新行? - How to add new row on table to localstorage? 使用 JavaScript 将商品添加到购物篮并存储在 localStorage 中 - Add items to basket and store in localStorage with JavaScript 如何使用 Javascript 将暗模式添加到 Localstorage - How to add Dark mode to Localstorage using Javascript javascript localstorage和angular localstorage($ localstorage)之间的区别? - difference between javascript localstorage and angular localstorage($localstorage)? 如何将JavaScript数组添加到localStorage中以进行临时数据存储? - How to add JavaScript array into localStorage for temporary data store? 向 localStorage 添加多个值,然后在 JavaScript 中随机检索一个 - Add multiple values to localStorage and then retrieve a random one in JavaScript 如果 localStorage 值存在而无需重新加载页面,则 Javascript 添加类 - Javascript add class if localStorage value exists without reloading page 我想从存储在本地存储中的javascript表中删除一行 - i want to delete a row from table in javascript which stored in localstorage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM