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

时间:2019-04-30 00:29:11

标签: javascript

我的程序需要将多个复选框的值存储在会话存储中(选中或未选中),以便当用户返回此页面时,它们已经被选中或未被选中。我发现该问题的所有解决方案都涉及到我们不应该使用的JQuery。

这是复选框的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>

2 个答案:

答案 0 :(得分:0)

因此,在Vanilla JS中,这就是如何在给定的字段集中获取所有检查的输入的方法。如果您可以更详细地更新问题,以及有关如何保存到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);

答案 1 :(得分:0)

谢谢,但我解决了

final Schema schema = new Schema.Parser().parse(new File("sample.avsc"));            
File file = new File("sample.avro");

//read the avro file to GenericRecord
final GenericDatumReader<GenericRecord> genericDatumReader = new GenericDatumReader<>(schema);
final DataFileReader<GenericRecord> genericRecords = new DataFileReader<>(file, genericDatumReader);

DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);

while (genericRecords.hasNext()) {
    //serialize GenericRecords
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder binaryEncoder = EncoderFactory.get().binaryEncoder(out, null);
    writer.write(genericRecords.next(), binaryEncoder);
    binaryEncoder.flush();
    out.close();

    // TODO: send out.toByteArray() to kafka
}

// TODO: kafkaProducer.flush();