JQurey案例

 步骤一:创建页面(student_add.html) 

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- 导入jquery -->
    <script type="text/javascript" src="js/jquery-3.3.1.js"></script>
</head>
<body>
    <form>
        编号:<input type="text" id="id"/> <br/>
        姓名:<input type="text" id="name"/> <br/>
        年龄<input type="text" id="age"/> <br/>
        性别:<input type="text" id="sex"/> <br/>
        <input type="button" value="添加学生"/> <br/>
    </form>
</body>
</html>
  • 步骤二:给添加按钮绑定点击事件

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- 导入jquery -->
    <script type="text/javascript" src="js/jquery-3.3.1.js"></script>
    <script>
        // 页面加载成功
        $(function() {
            // 给按钮绑定事件
            $("#bid").click(function(){
                // 添加功能
            });
        })
    </script>
</head>
<body>
    <form>
        编号:<input type="text" id="id"/> <br/>
        姓名:<input type="text" id="name"/> <br/>
        年龄<input type="text" id="age"/> <br/>
        性别:<input type="text" id="sex"/> <br/>
        <input type="button" id="bid" value="添加学生"/> <br/>
    </form>
</body>
</html>

步骤三:提交操作(获得用户输入的数据、发送ajax进行提交、成功跳转到列表页面、失败提示)

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- 导入jquery -->
    <script type="text/javascript" src="js/jquery-3.3.1.js"></script>
    <script>
        // 页面加载成功
        $(function() {
            // 1.1 给按钮绑定事件
            $("#bid").click(function(){
                // 1.2 添加功能
                // 1) 获得用户输入的数据
                var student = {
                    "id" : $("#id").val(),
                    "name" : $("#name").val(),
                    "age" : $("#age").val(),
                    "sex" : $("#sex").val()
                }
                // console.info(student)
                // 2) 发送ajax进行提交
                $.ajax({
                    "url":"http://data.javaliang.com/data/jack/student",
                    "type":"post",
                    "contentType":"application/json;charset=UTF-8",
                    "data": JSON.stringify(student),
                    "dataType":"json",
                    "success":function(data){
                        // 判断
                        if(data.code == 1){
                            // 成功
                            alert(data.message)
                            // 跳转到列表页面
                            location.href = "student_list.html"
                        } else {
                            // 失败
                            alert(data.message)
                        }
                       
                    },
                    "error":function(){
                        // 失败
                        alert("操作失败")
                    }
                })

            });
        })
    </script>
</head>
<body>
    <form>
        编号:<input type="text" id="id"/> <br/>
        姓名:<input type="text" id="name"/> <br/>
        年龄<input type="text" id="age"/> <br/>
        性别:<input type="text" id="sex"/> <br/>
        <input type="button" id="bid" value="添加学生"/> <br/>
    </form>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值