-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
45 lines (40 loc) · 1.52 KB
/
index.html
File metadata and controls
45 lines (40 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>輸入顯示範例</title>
</head>
<body>
<h1>請輸入文字:</h1>
<form id="inputForm">
<input type="text" id="inputText" name="inputText" placeholder="輸入點什麼..." required>
<button type="submit">提交</button>
</form>
<div id="output"></div>
<script>
// 監聽表單提交事件
document.getElementById('inputForm').addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表單的默認提交動作
// 獲取輸入框的值
const inputText = document.getElementById('inputText').value;
// 使用 fetch 發送異步 POST 請求
fetch('/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: new URLSearchParams({
inputText: inputText
})
})
.then(response => response.text()) // 將伺服器回應解析為文字
.then(data => {
// 將回應的文字顯示在頁面的 output div 中
document.getElementById('output').innerHTML = data;
})
.catch(error => console.error('Error:', error));
});
</script>
</body>
</html>