首页 > 解决方案 > HTML 保存按钮,复选框保持状态

问题描述

假设我正在使用 HTML 来做一些它并不真正适合的事情并让我感到幽默。

我有以下代码示例,但有没有办法将复选框状态保存到下载的 HTML 中?或者有没有人遇到过可以做到这一点的东西?

非常感谢,

<body>

<h1>Show Checkboxes</h1>

  <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
  <label for="vehicle1"> I have a bike</label><br>
  <input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
  <label for="vehicle2"> I have a car</label><br>
  <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
  <label for="vehicle3"> I have a boat</label><br><br>

<a href ="#" id="donwload-link" onClick="myFunction()">download html content</a>

<script>
function myFunction() {
  var content = document.documentElement.innerHTML;
  download(content, "index", "html")

}
function download(content, fileName, fileType) {
  var link = document.getElementById("donwload-link");
  var file = new Blob([content], {type: fileType});
  var donwloadFile = fileName + "." + fileType;
  link.href = URL.createObjectURL(file);
  link.download = donwloadFile
}
</script>
</body>
</html>

标签: html

解决方案


推荐阅读