gpt4 book ai didi

javascript - 如何自定义此复选框在页面刷新后保持选中状态

转载 作者:行者123 更新时间:2023-12-02 21:41:24 25 4
gpt4 key购买 nike

我正在尝试创建一个复选框列表,即使在我之前学到的一些 JS 的帮助下刷新后,该列表仍然保持选中状态。但当我现在检查它时,似乎我仍然需要对其进行一些调整以满足我的需求。

我现在主要关心的是如何自定义下面的代码

没有。 1 如何使下列项目以列表格式列出并保留复选框但去掉项目符号?例如:

复选框 1

复选框2

复选框3

复选框 4

(但不是每个项目之间的这种间距,更像是 Ctrl 或 Shift+ Enter 类型的间距)

没有。 2 如何在文本中添加链接?例如:

Checkbox 1

Checkbox 2

Checkbox 3

Checkbox 4

我知道这是一个简单的任务,尽管由于我不太擅长,我无法弄清楚如何自己调整它。我尝试过(也许不够努力)但失败了,所以我最终寻求帮助来弄清楚这段代码。因此,非常感谢任何可以在这里帮助我的人。

<!doctype html>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<div class="wrapper">
<header>
<div class="container">
<h3 class="col-lg-9">One Year Through</h3>

</div>
</header>
<div class="container">
<div class="alert alert-success">
<p>This demonstrates how to save the state of a checkbox between page refreshes. Click the checkboxes below and then refresh the page to see how they respond</p>
<p>The solution uses a class <b>save-cb-state</b> to denote which checkboxes must be persisted</p>
</div>
<ul class="list-group">
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox" value="yes"> Checkbox1</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox2" value="yes"> Checkbox 2</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox3" value="yes">Checkbox 3</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox4" value="yes"> Checkbox 4</li>
</ul>
</div>
</div>


<script src="http://code.jquery.com/jquery.js"></script>
<!-- This JavaScript file is required to load the XpressDox interview as well as the code required to run it -->

<script>
// Avoid scoping issues by encapsulating code inside anonymous function
(function() {
// variable to store our current state
var cbstate;

// bind to the onload event
window.addEventListener('load', function() {
// Get the current state from localstorage
// State is stored as a JSON string
cbstate = JSON.parse(localStorage['CBState'] || '{}');

// Loop through state array and restore checked
// state for matching elements
for(var i in cbstate) {
var el = document.querySelector('input[name="' + i + '"]');
if (el) el.checked = true;
}

// Get all checkboxes that you want to monitor state for
var cb = document.getElementsByClassName('save-cb-state');

// Loop through results and ...
for(var i = 0; i < cb.length; i++) {

//bind click event handler
cb[i].addEventListener('click', function(evt) {
// If checkbox is checked then save to state
if (this.checked) {
cbstate[this.name] = true;
}

// Else remove from state
else if (cbstate[this.name]) {
delete cbstate[this.name];
}

// Persist state
localStorage.CBState = JSON.stringify(cbstate);
});
}
});
})();
</script>
</body>
</html>

最佳答案

这可能是 localStorage 持久状态的问题...

这个:

cbstate = JSON.parse(localStorage['CBState'] || '{}');
...
localStorage.CBState = JSON.stringify(cbstate);

应该是这样的:

cbstate = JSON.parse(localStorage.getItem('CBState')) || {}
...
localStorage.setItem('CBState', JSON.stringify(cbstate))

CSS 应该是这样的:

.list-group {
list-style-type: none;
padding-left: 0;
}

HTML 应该是这样的:

<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox" value="yes">
<a href="https://example.com">Checkbox 1</a>
</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox2" value="yes">
<a href="https://example.com">Checkbox 2</a>
</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox3" value="yes"
<a href="https://example.com">Checkbox 3</a>
</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox4" value="yes">
<a href="https://example.com">Checkbox 4</a>
</li>

关于javascript - 如何自定义此复选框在页面刷新后保持选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60359097/

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