我的账户
猩码学苑

专注C++开发菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

巨朝阳-20221130-Ajax02

[复制链接]
云云学员认证 发表于 2022-12-1 09:02:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 100%;
            height: 100px;

        }
    </style>
</head>

<body>
    <div>
        姓名:<input type="text" id="userName" autofocus>
        <span id="tip" style="color:red"></span><br>
        密码:<input type="password" id="psw"><br>
        手机:<input type="text" id="tel"><br>
        年龄:<input type="email" id="age"><br>
        性别:<input type="text" id="sex"><br>
        查询:<input type="text" id="ipt">
        <button id="btn">查询</button>
        <button id="add">新增</button>
        <table>
            <thead>
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>电话</th>
                </tr>
            </thead>
            <tbody id="tbody">

            </tbody>
    </div>
            <script>
                const userName = document.querySelector('#userName');
                const psw = document.querySelector('#psw');
                const tel = document.querySelector('#tel');
                const age = document.querySelector('#age');
                const sex = document.querySelector('#sex');
                const btn = document.querySelector('#btn');
                const add = document.querySelector('#add');
                const tip = document.querySelector('#tip');
                const tbody = document.querySelector('#tbody');
                fn()
                function fn() {

                    var xhr = new XMLHttpRequest();
                    xhr.open('get', 'http://121.89.216.9/list');
                    xhr.send();
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4) {
                            let shuju = JSON.parse(xhr.responseText);
                            let src = shuju.data;
                            let html = ''
                            // console.log(shuju.code)
                            if (shuju.code == 0) {
                                // console.log(11111)
                                src.forEach(function (item, index) {
                                    html += `
                               <tr>
                               <td>${item.id}</td>
                               <td>${item.name}</td>
                               <td>${item.sex}</td>
                               <td>${item.age}</td>
                               <td>${item.son}</td>
                               <td>${item.tel}</td>
                               </tr>
                               `

                                })
                                //    console.log(html)
                                tbody.innerHTML = html
                            }
                        }
                    }
                }
                btn.addEventListener('click',function(){
                let id=ipt.value;
                // console.log(id)
                var cha=new XMLHttpRequest();
                cha.open('get',`接口${id}`);
                cha.send()
                cha.onreadystatechange=function(){
                    let html=''
                    if(cha.readyState==4){
                        if(JSON.parse(cha.responseText).code==0){
                            let shu=JSON.parse(cha.responseText).data
                        // console.log(1111111)
                        html+=`
                        
                               <tr>
                               <td>${shu.id}</td>
                               <td>${shu.name}</td>
                               <td>${shu.sex}</td>
                               <td>${shu.age}</td>
                               <td>${shu.tel}</td>
                               </tr>
                              
                        `
                    }
                    tbody.innerHTML=html

                    }
                }
            })
                add.addEventListener('click',function(){
                    if(!!userName.value&&!!psw.value&&!!tel.value&&!!age.value&&sex.value){
                        let url=`接口${userName.value}&age=${age.value}&sex=${sex.value}&password=${psw.value}&tel=${tel.value}`
                        var zeng=new XMLHttpRequest();
                        zeng.open('get',url);
                        zeng.send();
                        zeng.onreadystatechange=function(){
                            if(zeng.readyState==4){
                                // console.log(JSON.parse(zeng.responseText))
                                if(JSON.parse(zeng.responseText).mes=='成功'){
                                    fn()
                            }
                            }
                        }
                    }
                })
            </script>
</body>

</html>
回复

使用道具 举报

关注0

粉丝0

帖子39

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注C++菁英教育

客服电话:18009298968

客服时间:9:00-21:00

猩码学苑 - 专注C++开发菁英教育!( 陕ICP备2025058934号-1 )

版权所有 © 陕西菁英数字科技有限公司 2023-2026