gpt4 book ai didi

javascript - 多次将输入添加到本地存储中

转载 作者:行者123 更新时间:2023-11-28 06:16:55 25 4
gpt4 key购买 nike

重点是,我想通过一些下拉菜单和很少的输入(目前)将一些数据多次保存到本地存储中。我可以从输入中获取所有值,并在 console.log 中看到它。我想将输入保存到 localstorage 作为对象(单击按钮 Spremi),并在更改选项后再次单击以将另一个对象保存到 lcoalsotrage 等中。

enter image description here

浏览.html

<ion-view view-title="Browse">
<ion-content>
<select id="vrstaribe" ng-model="selekt" ng-options="r as r for r in ribe" selected>
<option value="">Vrsta ribe</option>
</select>
<label class="item item-input">
<input id="tezina" type="number" placeholder="Tezina">
</label>
<label class="item item-input">
<input id="mamac" type="text" placeholder="Mamac">
</label>
<button class="button button-positive" ng-click="spremi()">Spremi</button>
</ion-content>
</ion-view>

contollers.js

.controller('SpremiCtrl', function($scope) {
var ulov = {vrstaribe: '', tezina: '', mamac: '' };
var popisulova = [];

$scope.ribe = ["Saran", "Stuka", "Som"];

$scope.spremi = function() {

var vr = document.getElementById('vrstaribe');
var rib = vr.options[vr.selectedIndex].text;
var tez = document.getElementById('tezina').value;
var mam = document.getElementById('mamac').value;
console.log("Riba : " + rib + '\n' + "Težina : " + tez + '\n' + "Mamac : " + mam);
ulov.vrstaribe = rib;
ulov.tezina = tez;
ulov.mamac = mam;
popisulova.push(ulov);
console.log(ulov);

localStorage.setItem('ulov', JSON.stringify(ulov));
var vrati = localStorage.getItem('ulov');

//console.log('Ulov: ', JSON.parse(vrati));
console.log(ulov);

}

})

最佳答案

这将解决您的问题,但是您还有很多其他问题,并且您没有以“Angular 方式”进行编码。

$scope.spremi = function() {

var vr = document.getElementById('vrstaribe');
var rib = vr.options[vr.selectedIndex].text;
var tez = document.getElementById('tezina').value;
var mam = document.getElementById('mamac').value;
console.log("Riba : " + rib + '\n' + "Težina : " + tez + '\n' + "Mamac : " + mam);
ulov.vrstaribe = rib;
ulov.tezina = tez;
ulov.mamac = mam;

var fromStorage = localStorage.getItem('popisulova') || '[]';
var vrati = JSON.parse(fromStorage);
vrati.push(ulov);

localStorage.setItem('popisulova', JSON.stringify(vrati));

console.log(ulov);

}

关于javascript - 多次将输入添加到本地存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35885521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com