Я внес некоторые изменения в ваш HTML и Javascript.
По соображениям безопасности: код, не работающий во фрагменте, попробуйте здесь: https://jsfiddle.net/h7r0o2fg/12/
В HTML я добавил values
в inputs
И в сценарии я добавил условие переключателя.
Полагаю, у вас есть логика для вызова метода save()
, я вызывал его вручную.Затем, обновив страницу, я вызывал функцию load_()
.
function toggleAttributes(checkbox, radios, attribute, attributeValue) {
for (var i = 0; i < radios.length; i += 1) {
// If checkbox is checked, set the attribute and the attribute value. If not, remove the attribute
checkbox.checked === true ? radios[i].setAttribute(attribute, attributeValue) : radios[i].removeAttribute(
attribute);
}
}
function toggleRadios(el, id) {
var radiosSelector = `#${id} input[type='radio']`,
container = document.getElementById(id),
radios = document.querySelectorAll(radiosSelector);
container.classList.toggle("hide");
toggleAttributes(el, radios, "required", "");
}
var i;
var checkboxes = document.querySelectorAll('input[type=checkbox]');
var alertTxt = [];
function save() {
alertTxt = [];
var radios = document.querySelectorAll('input[type="radio"]');
for (i = 0; i < checkboxes.length; i++) {
console.log(checkboxes[i].id, checkboxes[i].checked, checkboxes.length);
localStorage.setItem(checkboxes[i].id, checkboxes[i].checked);
if(checkboxes[i].checked == true){
alertTxt.push(checkboxes[i].value + "(checked checkboxes)");
}
}
for (i = 0; i < radios.length; i++) {
localStorage.setItem(radios[i].id, radios[i].checked);
if(radios[i].checked == true){
alertTxt.push(radios[i].value + "(checked radiobutton)");
}
}
alert(alertTxt);
}
function load_() {
for (i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = localStorage.getItem(checkboxes[i].id) === 'true' ? true : false;
if (checkboxes[i].checked) {
var container = '#' + checkboxes[i].dataset.target;
document.querySelector(container).classList.toggle("hide");
var radios = document.querySelectorAll('#' + checkboxes[i].dataset.target + ' input[type="radio"]');
for (j = 0; j < radios.length; j++) {
console.log(localStorage.getItem(radios[j].id));
radios[j].checked = localStorage.getItem(radios[j].id) === 'true' ? true : false;
}
}
}
}
// self executing function here
(function () {
load_();
})();
.hide {
display: none;
}
<p>
Wybierz dania:
</p>
<div class="custom-control custom-checkbox custom-control-inline">
<input type="checkbox" class="custom-control-input" id="checkGlowne" data-target="pierwsze" value="pierwsze" onclick="toggleRadios(this, 'pierwsze')">
<label class="custom-control-label" for="checkGlowne">Glowne</label>
</div>
<div class="custom-control custom-checkbox custom-control-inline">
<input type="checkbox" class="custom-control-input" id="checkZupy" data-target="zupy" value="checkZupy" onclick="toggleRadios(this, 'zupy')">
<label class="custom-control-label" for="checkZupy">Zupy</label>
</div>
<p>
Pierwsze dania:
</p>
<div id="pierwsze" class="hide">
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="piers" name="pierwsze" value="piers">
<label class="custom-control-label" for="piers">Pierś z kaczki</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="stek" name="pierwsze" value="stek">
<label class="custom-control-label" for="stek">Stek z antrykotu</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="sandacz" name="pierwsze" value="sandacz">
<label class="custom-control-label" for="sandacz">Sandacz</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="karas" name="pierwsze" value="karas">
<label class="custom-control-label" for="karas">Karaś</label>
</div>
</div>
<div id="zupy" class="hide">
<p>
Zupy:
</p>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="rosol" name="zupy" value="karas">
<label class="custom-control-label" for="rosol">Rosół z kaczki</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="zurek" name="zupy" value="zurek">
<label class="custom-control-label" for="zurek">Żurek</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" class="custom-control-input" id="kokosowa" name="zupy" value="kokosowa">
<label class="custom-control-label" for="kokosowa">Zupa kokosowa</label>
</div>
</div>
<input type="button" value="save" onclick="save()">