【发布时间】:2017-04-17 13:12:34
【问题描述】:
我正在使用 Java、Thymeleaf、Spring、HTML 和一些 CSS 在 Eclipse 中开发一个购物清单 Web 应用程序。我使用 HTML 将复选框列表编码到应用程序中,因此当我完成某个列表时,我可以单击复选框并在文本中添加一行。但是,当我刷新页面时,复选框不会保持选中状态。我希望能够通过页面刷新保持该框处于选中状态。我已经尝试了来自其他 Stack Overflow 问题的各种不同代码位,例如将 Javascript 编码到 HTML 页面中,但到目前为止似乎没有任何效果。这是我的 HTML 代码:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layouts/basic">
<link rel="stylesheet" th:href="@{/templates.css/main.css}" />
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 15px;
}
.strikethrough:checked+.strikeThis {
text-decoration: line-through
}
</style>
</head>
<body layout:fragment="content">
<div class="row">
<h1>Shopping Lists</h1>
<!-- a simple button for later to add shopping lists -->
<a href="/" th:href="@{|/ListsofLists/add|}"><input type="button"
value="Add list" /></a>
<table>
<thead>
<tr>
<th>Name</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody class="list">
<tr th:each="list : ${lists}">
<td><input type="checkbox" name="check" class="strikethrough"
value="1" /> <label for="check" class="strikeThis"
th:text="${list.name}"> Name </label></td>
<!-- <td th:text="${list.name}"></td> -->
<td><a href="/" th:href="@{|/ListsofLists/${list.id}|}">Show</a></td>
<td><form method="POST">
<input type="hidden" name="listId" th:value="${list.id}" />
<button type="submit">Delete</button>
</form></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
我将尝试回答尽可能多的问题,以便弄清楚这个问题。抱歉,如果这是相当简单的事情,我是编码新手,还在学习。
【问题讨论】:
-
有很多方法可以做到这一点,一个简单的可能是 cookie
-
您可以在控制器类中使用会话属性
标签: javascript java html checkbox thymeleaf