简体   繁体   English

在会话存储中保留多个复选框状态

[英]Persisting multiple checkbox states in session Storage

My program needs to store the value of multiple checkboxes in session Storage (checked or not checked), so that when the user returns to this page they are already checked or not checked. 我的程序需要在会话存储(已选中或未选中)中存储多个复选框的值,以便当用户返回此页面时,它们已经被选中或未被选中。 All the solutions to this problem that I have found involve the use of JQuery which we aren't supposed to use. 我发现这个问题的所有解决方案都涉及使用我们不应该使用的JQuery。

This is the HTML for the checkboxes 这是复选框的HTML

<fieldset>
    <legend>Skills List</legend>
    <p> <label for="teamwork">Teamwork</label>
            <input type="checkbox" id="teamwork" name="Skill[]" value="Teamwork" checked="checked"/>
        <label for="rubyskills">Ruby Experience</label>
            <input type="checkbox" id="rubyskills" name="Skill[]" value="Rubyskills"/>
        <label for="efficiency">Efficiency</label>
            <input type="checkbox" id="efficiency" name="Skill[]" value="Efficiency"/>
        <label for="communication">Communication</label>
            <input type="checkbox" id="communication" name="Skill[]" value="Communication"/>
        <label for="other">Other</label>
        <input type="checkbox" id="other" name="Skill[]" value="other"/>
    </p>

so, in vanilla JS this is how you would go about getting all of the checked inputs in your given fieldset. 所以,在vanilla JS中,您将如何获得给定字段集中的所有已检查输入。 If you could update your question with a bit more detail and code on how you are saving to session storage I can help further. 如果您可以更详细地更新您的问题,并且有关如何保存到session storage代码,我可以进一步提供帮助。

let fieldset = document.getElementById("fieldset1") // get fieldset element
let p = fieldset.children[1]; // the p tag
let pchildren = p.children; // the contents of the p tag
let inputs = []; // where we will hold the input elements that are checked
for (let x = 0; x < pchildren.length; x++) { // looping through to get only the inputs that are checked
  if (pchildren[x].nodeName == "INPUT" && pchildren[x].checked) {
    inputs.push(pchildren[x]) // pushing into array
  }
}
console.log(inputs);

Thanks but I solved it 谢谢,但我解决了

function store_data() {
var teamwork = document.getElementById("teamwork").checked;
var rubyskills = document.getElementById("rubyskills").checked;
var efficiency = document.getElementById("efficiency").checked;
var communication = document.getElementById("communication").checked;

sessionStorage.setItem("teamwork", teamwork);
sessionStorage.setItem("ruby", rubyskills);
sessionStorage.setItem("efficiency", efficiency);
sessionStorage.setItem("communication", communication);
sessionStorage.setItem("other", other);
}


function prefill_form() {
if(sessionStorage.teamwork == "true") {
        document.getElementById("teamwork").checked = true;
    }
    if(sessionStorage.ruby == "true") {
        document.getElementById("rubyskills").checked = true;
    }
    if(sessionStorage.efficiency == "true") {
        document.getElementById("efficiency").checked = true;
    }
    if(sessionStorage.communication == "true") {
        document.getElementById("communication").checked = true;
    }
    if(sessionStorage.other == "true") {
        document.getElementById("other").checked = true;
    }

}       

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

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