Html 弹出窗体,提供用户输入信息,并把数据提交到指定的API

首次发布:2025-02-27

效果图如下

微信截图_20250227144724.png

以下是核心代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹出 HTML 窗体示例</title>
    <style>
        /* 隐藏默认的模态对话框 */
        #modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        /* 模态对话框的内容 */
        #modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            text-align: center;
        }

        /* 关闭按钮样式 */
        #close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            margin-top: -20px;
            margin-right: -10px;
        }

            #close:hover,
            #close:focus {
                color: black;
                text-decoration: none;
                cursor: pointer;
            }
    </style>
</head>

<body>
    <button onclick="openModal()">打开弹出窗体</button>

    <!-- 模态对话框 -->
    <div id="modal">
        <div id="modal-content">
            <span id="close">&times;</span>
            <h4>请留下您的邮箱,我们将在2小时内将文件发给您的邮箱</h4>
            <form id="myForm">
                <label for="email">邮箱:</label>
                <input type="email" id="email" name="email" required><br>
                <input type="submit" style="margin-top:10px;padding:10px 20px" value="提交">
            </form>
        </div>
    </div>

    <script>
        function openModal() {
            document.getElementById('modal').style.display = 'block';
        }
        
        document.getElementById('close').addEventListener('click', function () {
            document.getElementById('modal').style.display = 'none';
        });

        //点击窗体以外部分关闭窗体
        window.onclick = function (event) {
            if (event.target === document.getElementById('modal')) {
                document.getElementById('modal').style.display = 'none';
            }
        };

        const form = document.getElementById('myForm');
        form.addEventListener('submit', function (event) {
            // 阻止表单的默认提交行为
            event.preventDefault();

            const formData = new FormData(form);
            // 创建一个新的 XMLHttpRequest 对象
            const xhr = new XMLHttpRequest();
            // 初始化一个 POST 请求
            xhr.open('POST', 'https://www.luofenming.com/api/test', true);

            // 监听请求状态变化
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        // 请求成功,打印响应结果
                        console.log('请求成功:', xhr.responseText);
                    } else {
                        // 请求失败,打印错误信息
                        console.error('请求失败,状态码:', xhr.status);
                    }
                }
            };
            // 发送表单数据
            xhr.send(formData);
        });
    </script>
</body>

</html>

本文来自www.luofenming.com