jQuery---表单事件


<!DOCTYPE html>
<html lang="zh">
<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>
</head>
<body>

    <form action="" method="post">
        <p><label>账户:<input type="text" value="aaa"></label></p>
        <p><label>密码:<input type="text"></label></p>
        <p><input type="submit" value="登录"></p>
    </form>
    
    <script src="./jquery.js"></script>
    <script>
        
        // 1. 焦点事件
        $('input[type="text"]').focusin(function (event) {
            // 焦点刚刚进入时触发
            console.log(event.type)
        }).focus(function (event) {
            // 已经获得焦点时触发
            console.log(event.type)
        }).focusout(function (event) {
            // 刚要失去焦点时触发
            console.log(event.type)
        }).blur(function (event) {
            // 已经失去焦点时触发
            console.log(event.type)
        });

        // 2. change 事件
        $('input[type="text"]').change(function (event) {
            // 当输入框中的内容发生变化时触发
            console.log(event.type)
        }).on('input', function (event) {
            // 当在输入框中输入数据时会实时触发,使用 val() 方法获取 input 框中的数据
            console.log($(this).val())
            // console.log(event.type)
        }).select(function (event) {
            // 当输入框中的内容被选中时触发
            console.log(event.type)
        });

        // 3. submit 事件
        $('form[method="post"]').on('submit', function(event){
            // 表单被提交之后会自动地跳转页面(默认行为),跳转的目标是 action 属性中的URL,如果 action 属性为空,则把当前浏览器地址栏中的 URL 作为跳转的目标。
            // 怎样阻止表单的默认行为(不提交数据,并且不跳转页面)
            // 方法一:
            event.preventDefault()
            console.log('hahahah')
            // 方法二:不常用,某些浏览器可能不支持这种写法
            // return false;
        })

    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值