通过Ajax提交表单数据

监听表单提交事件:

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

 

 

 代码示例:

<!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="/form表单/jquery-3.6.0.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>
</body>
<script>
    $(function () {
        // 第一种方式
        // $('#f1').submit(function () {
        //     alert("监听到了")
        // })

        // 第二种方式
        $('#f1').on('submit', function () {
            alert('监听到了2')
        })

    })
</script>

</html>

 

posted @ 2022-04-28 20:40  今天穿秋裤了吗  阅读(144)  评论(0)    收藏  举报