jsp页面中怎么让输入框中的值再刷新之后在这个输入框中仍然存在,用代码表示
时间: 2024-02-27 14:56:59 浏览: 14
要让输入框中的值在页面刷新之后仍然存在,可以使用浏览器的本地存储(Local Storage)来实现。具体的做法是,每次用户输入完成后,将输入框的值存储到 Local Storage 中,在页面加载时从 Local Storage 中读取输入框的值并填充到输入框中。
以下是一个示例代码:
```
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>输入框值的本地存储示例</title>
<script>
// 在页面加载时从 Local Storage 中读取输入框的值
function loadInput() {
var input = document.getElementById("myInput");
input.value = localStorage.getItem("myInputValue") || "";
}
// 在输入框中输入完成后,将值存储到 Local Storage 中
function saveInput() {
var input = document.getElementById("myInput");
localStorage.setItem("myInputValue", input.value);
}
</script>
</head>
<body onload="loadInput()">
<form>
<input type="text" id="myInput" oninput="saveInput()">
</form>
</body>
</html>
```
在这个示例中,我们使用了一个名为 `myInputValue` 的 Local Storage 条目来存储输入框的值。在页面加载时,我们调用 `loadInput()` 函数从 Local Storage 中读取值并填充到输入框中。在输入框中输入完成后,我们调用 `saveInput()` 函数将输入框的值存储到 Local Storage 中。这样,在页面刷新之后,输入框中的值就会根据 Local Storage 中的值自动填充。