index.html 2.62 KB
Newer Older
wangkaixiong's avatar
wangkaixiong committed
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Task Manager</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        h1 {
            color: #333;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        li {
            padding: 10px;
            border: 1px solid #ddd;
            margin-bottom: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        button {
            background-color: #ff4d4d;
            color: white;
            border: none;
            padding: 5px 10px;
            cursor: pointer;
        }
        button:hover {
            background-color: #cc0000;
        }
        input {
            padding: 5px;
            width: 200px;
        }
    </style>
</head>
<body>
    <h1>Task Manager</h1>
    <form id="addTaskForm">
        <input type="text" id="taskInput" placeholder="Enter a new task" required>
        <button type="submit">Add Task</button>
    </form>
    <ul id="taskList">
        {% for task in tasks %}
        <li>
            <span>{{ task }}</span>
            <button onclick="removeTask('{{ task }}')">Remove</button>
        </li>
        {% endfor %}
    </ul>

    <script>
        // 添加任务
        document.getElementById('addTaskForm').addEventListener('submit', function (e) {
            e.preventDefault();
            const task = document.getElementById('taskInput').value;
            fetch('/add', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `task=${encodeURIComponent(task)}`,
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    location.reload(); // 刷新页面
                }
            });
        });

        // 删除任务
        function removeTask(task) {
            fetch('/remove', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `task=${encodeURIComponent(task)}`,
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    location.reload(); // 刷新页面
                }
            });
        }
    </script>
</body>
</html>