首页 > 其他分享 >10.31

10.31

时间:2023-11-02 19:33:43浏览次数:40  
标签:const 10.31 value error input document data

今天我们再来实现上述个人信息添加的前端代码。

 1、add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加个人信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        #root {
            margin: 20px;
        }

        h2 {
            font-size: 18px;
            margin-bottom: 10px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input, select {
            width: 300px;
            padding: 5px;
            font-size: 16px;
        }

        button {
            display: block;
            margin-top: 10px;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<h1 style="text-align: center">添加用户信息</h1>
<div id="root" style="border: 1px solid black">
    <h2>增加人员</h2>
    <form id="addForm">
        <label for="updateId">学号:</label>
        <input type="text" id="updateId" name="id" required oninput="limitInput(this)" maxlength="8">
        <label for="updateName">姓名:</label>
        <input type="text" id="updateName" name="name" required>
        <label for="updateAge">年龄:</label>
        <input type="number" id="updateAge" name="age" min="1" max="150">
        <label>性别:</label>
        <div id="gender">
            <label><input type="radio" name="gender" value="男"> 男</label>
            <label><input type="radio" name="gender" value="女"> 女</label>
        </div>
        <label>兴趣爱好:</label>
        <div id="hobbies">
            <label><input type="checkbox" name="hobbies" value="阅读"> 阅读</label>
            <label><input type="checkbox" name="hobbies" value="音乐"> 音乐</label>
            <label><input type="checkbox" name="hobbies" value="运动"> 运动</label>
            <label><input type="checkbox" name="hobbies" value="旅行"> 旅行</label>
            <label><input type="checkbox" name="hobbies" value="烹饪"> 烹饪</label>
        </div>
        <label for="updateMajor">专业:</label>
        <select id="updateMajor" name="major" required>
            <option value="">请选择</option>
            <option value="计算机科学与技术">计算机科学与技术</option>
            <option value="软件工程">软件工程</option>
            <option value="网络工程">网络工程</option>
            <option value="信息工程">信息工程</option>
            <option value="数字媒体技术">数字媒体与技术</option>
            <option value="人工智能">人工智能</option>
        </select>
        <button type="submit">添加人员</button>

    </form>
    <div>
        <a href="index.html">
            <button>返回主界面</button>
        </a>
    </div>
</div>
</body>
<script>
    document.getElementById("addForm").addEventListener("submit", function (event) {
        event.preventDefault();
        const studentid = document.getElementById("updateId");
        const name = document.getElementById("updateName");
        const age = document.getElementById("updateAge");
        const genderRadios = document.querySelectorAll('input[name="gender"]');
        let gender;

        genderRadios.forEach(radio => {
            if (radio.checked) {
                gender = radio.value;
                alert(gender);
            }
        });

        const hobbies = document.querySelectorAll('input[name="hobbies"]:checked');
        const hobbyList = [];
        for (const hobby of hobbies) {
            hobbyList.push(hobby.value);
        }
        const major = document.getElementById("updateMajor");
        fetch('person/add',
            {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    studentid: studentid.value,
                    name: name.value,
                    age: age.value,
                    gender: gender,
                    hobby: hobbies.value,
                    major: major.value
                })
            })
            .then(res => res.json())
            .then(data => {
                if (data.msg === 'success') {
                    alert("添加成功");
                    console.log(data);
                } else {
                    alert("添加失败  " + data.msg);
                }
            })
            .catch(error => {
                alert("请求失败,请重试");
                console.error(error);
            });
    });
</script>
<script>
    function limitInput(input) {
        const value = input.value;
        if (value.length > 8) {
            input.value = value.slice(0, 8);
        }
        if (value.length < 4 || value.slice(0, 4) !== '2022') {
            input.setCustomValidity('学号必须是八位且前四位为2022');
        } else {
            input.setCustomValidity('');
        }
    }
</script>
</html>

2、delete.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>删除个人信息</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            text-align: center;
            border: 1px solid #ccc;
            padding: 500px;
        }

        button {
            margin-top: 10px;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="container">
    <h1 style="text-align: center">删除信息</h1>
    <form id="deleteform">
        <label>学号</label>
        <input type="text" id="studentid" required oninput="limitInput(this)" maxlength=8">
        <br>
        <button type="submit" name="提交">提交</button>
    </form>
    <div>
        <a href="index.html">
            <button>返回主界面</button>
        </a>
    </div>
</div>
</body>
<script>
    document.getElementById("deleteform").addEventListener('submit', function (event) {
        event.preventDefault();
        const studentid = document.getElementById("studentid");
        fetch('person/delete/' + studentid.value, {
            method: 'DELETE',
            headers: {
                'Content-type': 'application.json'
            },
            body: JSON.stringify(
                {
                    studentid: studentid.value
                }
            )
        })
            .then(response => response.json())
            .then(data => {
                if (data.msg == 'success') {
                    alert("删除成功");
                } else {
                    alert("删除失败 " + data.msg);
                }
            })
            .catch(error => {
                alert("请求失败,请重试");
                console.error(error);
            })
    });
</script>
<script>
    function limitInput(input) {
        const value = input.value;
        if (value.length > 8) {
            input.value = value.slice(0, 8);
        }
        if (value.length < 4 || value.slice(0, 4) !== '2022') {
            input.setCustomValidity('学号必须是八位且前四位为2022');
        } else {
            input.setCustomValidity('');
        }
    }
</script>
</html>

3、index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人员管理系统</title>
    <style>
        .form {
            width: 600px;
            margin: 0 auto;
            /*border: 1px solid red;*/
        }

        .form table {
            margin: 0 auto;
        }

        .form table tr td {
            width: 100px;
            height: 30px;
            border: 1px solid #000;
            text-align: center;
        }

        button {
            display: block;
            margin-top: 10px;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<h1 style="text-align: center">个人信息管理</h1>
<!-- 其余代码不变 -->
<div class="form">
    <table border="1px" cellspacing="0" width="600px">
        <tr>
            <th>编号</th>
            <th>功能</th>
        </tr>
        <tr>
            <td>1</td>
            <td>
                <a href="add.html" target="_blank">
                    <button>添加信息</button>
                </a>
            </td>
        </tr>
        <tr>
            <td>2</td>
            <td><a href="delete.html" target="_blank">
                <button>删除信息</button>
            </a>
            </td>
        </tr>
        <tr>
            <td>3</td>
            <td>
                <a href="update.html" target="_blank">
                    <button>修改信息</button>
                </a>
            </td>
        </tr>
        <tr>
            <td>4</td>
            <td>
                <a href="select.html" target="_blank">
                    <button>查询信息</button>
                </a>
            </td>
        </tr>
    </table>
</div>
</body>
</html>

4、select.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询信息</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            text-align: center;
            border: 1px solid #ccc;
            padding: 500px;
        }

        button {
            margin-top: 10px;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        h1 {
            margin-top: 0; /* 防止标题上方的空白 */
        }
    </style>
</head>
<body>'
<div class="container">
    <h1 style="text-align: center">查询信息</h1>
    <form id="selectform">
        <select id="selectway">
            <option value="id">按照学号查询,学号为八位,开头须为2022</option>
            <option value="name">按照姓名查询</option>
            <option value="all">查询所有信息</option>
        </select>
        <br>
        <input type="text" id="information">
        <button type="submit" name="提交" required>提交</button>
    </form>
    <div id="tablecontainer">

    </div>
    <div>
        <a href="index.html">
            <button>返回主界面</button>
        </a>
    </div>
</div>
</body>
<script>
    document.getElementById("selectform").addEventListener('submit', function (event) {
        event.preventDefault();
        const selectway = document.getElementById("selectway").value;
        const information = document.getElementById("information").value;

        fetchAndDisplayData(selectway, information);
    });
</script>
<script>
    function fetchAndDisplayData(selectway, information) {
        if (selectway === "id") {
            fetch(`person/getById/${information}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application.json'
                },
            })
                .then(response => response.json())
                .then(data => {
                    if (data.msg === 'success') {
                        generateTable(data.data, 1);
                    } else {
                        alert("此结果不存在");
                    }
                })
                .catch(error => {
                    alert("请求失败,请重试");
                    console.error(error);
                });
        } else if (selectway === "name") {
            fetch(`person/getByName/${information}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application.json'
                },
            })
                .then(response => response.json())
                .then(data => {
                    if (data.msg === 'success') {
                        alert(data.data.name);
                        generateTable(data.data, 2);
                    } else {
                        alert("此结果不存在");
                    }
                })
                .catch(error => {
                    alert("请求失败,请重试");
                    console.error(error);
                });
        } else if (selectway === "all") {
            fetch('person/getAll', {
                method: 'GET',
                headers: {
                    'Content-Type': 'application.json'
                },
            })
                .then(response => response.json())
                .then(data => {
                    if (data.msg === 'success') {
                        generateTable(data.data, 3);
                    } else {
                        alert("此结果不存在");
                    }
                })
                .catch(error => {
                    alert("请求失败,请重试");
                    console.error(error);
                });
        }
    }

</script>
<script>
    function generateTable(data, way) {
        const tableContainer = document.getElementById("tablecontainer");

        // 清空 tableContainer 中的所有子节点
        while (tableContainer.hasChildNodes()) {
            tableContainer.removeChild(tableContainer.firstChild);
        }

        // if (way===1||(way===2)) {
        if (data.studentid) {
            // 查询方式是按学号查询
            console.log(11);
            const table = document.createElement("table");
            const tableBody = document.createElement("tbody");

            let row = document.createElement("tr");
            row.innerHTML = '<td>学号</td><td>姓名</td><td>年龄</td><td>性别</td><td>爱好</td><td>专业</td>';
            tableBody.appendChild(row);

            row = document.createElement("tr");
            row.innerHTML = `<td>${data.studentid}</td><td>${data.name}</td><td>${data.age}</td><td>${data.gender}</td><td>${data.hobby}</td><td>${data.major}</td>`;
            tableBody.appendChild(row);

            table.appendChild(tableBody);
            tableContainer.appendChild(table);
        } else {
            // if (data.length>1) {
            const table = document.createElement("table");
            const tableBody = document.createElement("tbody");
            let row = document.createElement("tr");
            row.innerHTML = '<td>学号</td><td>姓名</td><td>年龄</td><td>性别</td><td>爱好</td><td>专业</td>';
            tableBody.appendChild(row);
            // 查询方式是按姓名查询或多条查询
            for (let i = 0; i < data.length; i++) {
                row = document.createElement("tr");
                row.innerHTML = `<td>${data[i].studentid}</td><td>${data[i].name}</td><td>${data[i].age}</td><td>${data[i].gender}</td><td>${data[i].hobby}</td><td>${data[i].major}</td>`;
                tableBody.appendChild(row);

                table.appendChild(tableBody);
                tableContainer.appendChild(table);
            }
        }
    }
</script>
</html>

5、update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>更新信息</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            text-align: center;
            border: 1px solid #ccc;
            padding: 500px;
        }

        button {
            margin-top: 10px;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        h1 {
            margin-top: 0; /* 防止标题上方的空白 */
        }
    </style>
</head>
<body>
<div class="container">
    <h1 style="align-content: center">更新个人信息</h1>
    <form id="addForm">
        <label for="updateId">请输入您要修改信息的学号:</label>
        <br>
        <input type="text" id="updateId" name="id" required oninput="limitInput(this)" maxlength="8">
        <br>
        <label for="updateName">姓名:</label>
        <br>
        <input type="text" id="updateName" name="name" required>
        <br>
        <label for="updateAge">年龄:</label>
        <br>
        <input type="number" id="updateAge" name="age" min="1" max="150">
        <br>
        <label>性别:</label>
        <div id="gender">
            <label><input type="radio" name="gender" value="男"> 男</label>
            <label><input type="radio" name="gender" value="女"> 女</label>
        </div>
        <label>兴趣爱好:</label>
        <div id="hobbies">
            <label><input type="checkbox" name="hobbies" value="阅读"> 阅读</label>
            <label><input type="checkbox" name="hobbies" value="音乐"> 音乐</label>
            <label><input type="checkbox" name="hobbies" value="运动"> 运动</label>
            <label><input type="checkbox" name="hobbies" value="旅行"> 旅行</label>
            <label><input type="checkbox" name="hobbies" value="烹饪"> 烹饪</label>
        </div>
        <label for="updateMajor">专业:</label>
        <select id="updateMajor" name="major" required>
            <option value="">请选择</option>
            <option value="计算机科学与技术">计算机科学与技术</option>
            <option value="软件工程">软件工程</option>
            <option value="网络工程">网络工程</option>
            <option value="信息工程">信息工程</option>
            <option value="数字媒体技术">数字媒体与技术</option>
            <option value="人工智能">人工智能</option>
        </select>
        <br>
        <button type="submit">更改人员</button>
    </form>
    <div>
        <a href="index.html">
            <button>返回主界面</button>
        </a>
    </div>
</div>
</body>
<script>
    document.getElementById("addForm").addEventListener("submit", function (event) {
        event.preventDefault();
        const studentid = document.getElementById("updateId");
        const name = document.getElementById("updateName");
        const age = document.getElementById("updateAge");
        const genderRadios = document.querySelectorAll('input[name="gender"]');
        let gender;

        genderRadios.forEach(radio => {
            if (radio.checked) {
                gender = radio.value;
                alert(gender);
            }
        });

        const hobbies = document.querySelectorAll('input[name="hobbies"]:checked');
        const hobbyList = [];
        for (const hobby of hobbies) {
            hobbyList.push(hobby.value);
        }
        const major = document.getElementById("updateMajor");
        console.log(studentid + " " + name + " " + age + " " + gender + " " + hobbyList + " " + major + "  " + hobbyList.value);
        fetch('person/update',
            {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    studentid: studentid.value,
                    name: name.value,
                    age: age.value,
                    gender: gender,/**/
                    hobby: hobbyList,
                    major: major.value
                })
            })
            .then(res => res.json())
            .then(data => {
                if (data.msg === 'success') {
                    alert("更改成功");
                    console.log(data);
                } else {
                    alert("添加失败  " + data.msg);
                }
            })
            .catch(error => {
                alert("请求失败,请重试");
                console.error(error);
            });
    });
</script>
<script>
    function limitInput(input) {
        const value = input.value;
        if (value.length > 8) {
            input.value = value.slice(0, 8);
        }
        if (value.length < 4 || value.slice(0, 4) !== '2022') {
            input.setCustomValidity('学号必须是八位且前四位为2022');
        } else {
            input.setCustomValidity('');
        }
    }
</script>
</html>

 

标签:const,10.31,value,error,input,document,data
From: https://www.cnblogs.com/zzqq1314/p/17806116.html

相关文章

  • crypto 2023.10.31-11.05
    1.a.题目后面有"="就先猜一手base64编码,直接复制base64解码解密即可得到flagb.故直接用工具进行解密 2. a.因为是MD5加密,故直接用工具解密 3. a.因为是Url加密,故直接用工具解密 4. a.看题目像是凯撒密码,直接使用工具,并找到flag  5. a.因为key{}里面......
  • 10.31 NOIP模拟测试
    10.31NOIP模拟测试赛时先看题,T1有一点思路,T2是我不擅长的期望计数,但看起来还是可以试一试,T3数据范围看起来是NP,想了一下搜索但没有一下想出来,T4一眼大数据结构,最后做。T1想了一下前缀和,去上了个厕所,中途想出后缀和和前缀和比较,回去写看打样例发现不仅要比较相邻的,还要比......
  • 10.31
    10.31字符与字符串字符字符在计算机中以ASCII码进行存储(从0到127->对应7位二进制)字符A```Z```的ASCII码值从6590字符a```z```的ASCII码值从97122对应的⼤⼩写字符(a和A)的ASCII码值的差值是32数字字符09的ASCII码值从4857换⾏\n的ASCII值是:10第一位二进制位代表扩......
  • 每日总结10.31
    今天是十月的最后一天,也是传统的万圣节,这一天充满了特殊的氛围。上午,我参加了算法与数据结构的课程,今天的主题是图和森林。学习图是计算机科学中的重要部分,它们在网络、社交媒体、地理信息系统等众多领域都有广泛应用。通过学习图,我了解了图的基本概念和算法,这对于解决各种实际问......
  • 2023.10.31 USACO 2020 选做.md
    P6009Non-DecreasingSubsequencesP由于值域很小,dp的转移不难想到写成矩阵的形式。考虑维护矩阵的前缀积和逆前缀积。然而单次的矩阵乘已经达到\(O(k^3)\)超时了,但是我们发现其实矩阵非\(0\)的位置是\(O(k)\)个的,所以复杂度降到了\(O(k^2)\).关于逆矩阵,我们无需高斯......
  • 每日总结10.31
    Flink的优势包括:高度灵活的流式窗口,同时支持高吞吐、低延迟、高性能,支持有状态计算流数据的特征:注重数据的整体价值,不过分关注个别数据,数据快速持续到达流计算的处理流程包括:数据实时采集,实时查询服务,数据是实时计算典型的事件驱动型应用包括:异常检测,反欺诈,业务流程监控,基于规则......
  • 10.31 限滑
    我有异议证第一个ST-Link2到了,甚至没有调试针孔,只提供了焊盘....麻辣隔壁我还得用手按着才能刷程序。另一个GD32F103的ST-Linkv2昨天发货顺丰空运来的,今天应该能到。两家都是深圳的。到时候先试试GD32的能不能串口刷,能的话就先做手上这个STM32的。有没有人要预......
  • 2023.10.31
    运行超市抹零结账行为代码如下:1print("3107")2money=39.87+24.47+78.07#计算总金额3money_str=str(money)4print("商品总金额:"+money_str)5print("实收金额:{:.0f}".format(money))#进行抹零行为结果如下:计算学生成绩的分差和平均分代码如下:......
  • 10.31算法
    最长回文子串给你一个字符串s,找到s中最长的回文子串。如果字符串的反序与原始字符串相同,则该字符串称为回文字符串。 示例1:输入:s="babad"输出:"bab"解释:"aba"同样是符合题意的答案。示例2:输入:s="cbbd"输出:"bb"class Solution {public:    string longes......
  • 10.31-11.4 周末总结
    目录一、ATM项目二、编程思想1.面向过程2.面向对象三、对象与类四、类与对象的创建1.类的语法结构2.类的定义与调用1.定义类2.查看名称空间的方法1__dict__方法2点号运算......