Ajax提交表单数据

这篇博客介绍了如何在jQuery中监听表单提交事件,包括两种不同的方法,并展示了如何阻止表单的默认提交行为。同时,文章还详细解释了如何使用serialize()函数方便地获取表单数据,强调了在使用serialize()时需为表单元素设置name属性的重要性。
摘要由CSDN通过智能技术生成

监听表单提交事件

在jQuery中,可以使用以下两种方式,监听到表单的提交事件

$('#form').submit(function(e) {
    alert('监听到了表单的提交事件');
})

$('#form').on('submit', function(e) {
    alert('监听到了表单的提交事件');
})
<!DOCTYPE html>
<html lang="zh-CN">

<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="../../js/jQuery.js">
    </script>
</head>

<body>
    <form action="/login" id="f1">
        <input type="text" name="user_name">
        <input type="password" name="password">
        <button type="submit">提交</button>
    </form>
    <script>
        $(function() {
            //第一种方式
            // $('#f1').submit(function(object) {
            //     alert('监听到表单的提交')
            // })

            //第二种方式
            $('#f1').on('submit', function() {
                alert('监听到表单的提交');
            })
        })
    </script>
</body>

</html>

 

阻止表单默认提交行为

<!DOCTYPE html>
<html lang="zh-CN">

<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="../../js/jQuery.js">
    </script>
</head>

<body>
    <form action="/login" id="f1">
        <input type="text" name="user_name">
        <input type="password" name="password">
        <button type="submit">提交</button>
    </form>
    <script>
        $(function() {
            //第一种方式
            // $('#f1').submit(function(object) {
            //     alert('监听到表单的提交')
            // })
            //第二种方式
            $('#f1').on('submit', function() {
                alert('监听到表单的提交');
            })

            //组织表单默认提交行为和页面的跳转
            //第一种方式
            // $('#f1').submit(function(e) {
            //         e.preventDefault();
            //     })
            //第二种方式
            $('#f1').on('submit', function(e) {
                e.preventDefault();
                alert('阻止了表单的默认行为');
            })
        })
    </script>
</body>

</html>

 

快速获取表单中的数据

serialize()函数

为了简化表单中数据的获取操作,jQuery提供了serialize()函数,语法格式如下:

$(selector).serialize();

serialize()函数的好处: 可以一次性获取到列表中所有的数据

注意: 在使用serialize()函数快速获取表单数据时,必须为每个表单元素添加name属性

serialize()函数示例

<!DOCTYPE html>
<html lang="zh-CN">

<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>
</head>

<body>
    <form action="" id="form1">
        <input type="text" name="username">
        <input type="password" name="password">
        <button type="submit">提交</button>
    </form>
    <script src="../../js/jQuery.js"></script>
    <script>
        $(function() {
            $('#form1').on('submit', function(e) {
                e.preventDefault();
                var data = $(this).serialize();
                console.log(data);
            })
        })
    </script>
</body>

</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值