JQuery 课时16:JQuery表单事件

1、focus()-获得焦点事件

<!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>
    <script src="./jquery-3.6.0.js"></script>
</head>
<body>
    <form action="">
        <input id="target" type="text" value="field 1">
        <input  type="text" value="field 2">
    </form>
    <script>
        $(function(){
            // 当一个框点击时获得了焦点
            $("#target").focus(function(){
                alert("获得焦点");
            })

        })
    </script>
</body>
</html>

在这里插入图片描述

2、blur()-失去焦点事件

<!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>
    <script src="./jquery-3.6.0.js"></script>
</head>
<body>
    <form action="">
        <input id="target" type="text" value="field 1">
        <input  type="text" value="field 2">
    </form>
    <script>
        $(function(){
            // 当一个框点击时获得了焦点
            $("#target").blur(function(){
                alert("失去焦点");
            })

        })
    </script>
</body>
</html>

在这里插入图片描述

3、change()-一个元素的值改变就会触发,仅限于input、select、textarea

<!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>
    <script src="./jquery-3.6.0.js"></script>
</head>
<body>
    <form action="">
        <input id="target" type="text" value="field 1">
        <input  type="text" value="field 2">
    </form>
    <script>
        $(function(){
            $("#target").change(function(){
                alert("内容改变了");
            })

        })
    </script>
</body>
</html>

在这里插入图片描述

4、select(),当选择元素中的文本中触发,仅限于input type=text、select、textarea

<!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>
    <script src="./jquery-3.6.0.js"></script>
</head>
<body>
    <form action="">
        <input id="target" type="text" value="field 1">
        <input  type="text" value="field 2">
    </form>
    <script>
        $(function(){
            $("#target").select(function(){
                alert("选择fled1的内容");
            })

        })
    </script>
</body>
</html>

在这里插入图片描述

5、submit(),当用户提交表单时触发,只能用于form表单上

<!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>
    <script src="./jquery-3.6.0.js"></script>
</head>
<body>
    <form action="" id="target">
        <input type="submit" value="提交">
    </form>
    <script>
        $(function(){
            $("#target").submit(function(){
                alert("确定提交吗");
            })

        })
    </script>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值