0
点赞
收藏
分享

微信扫一扫

多步表单

微笑沉默 2024-09-16 阅读 19

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multi-Step Form</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            background-color: #f0f2f5;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .step {
            display: none;
        }
        .step.active {
            display: block;
        }
        .step-buttons {
            display: flex;
            justify-content: space-between;
        }
        button {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            font-size: 1em;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Multi-Step Form</h1>
        <form id="multiStepForm">
            <div class="step active">
                <h2>Step 1</h2>
                <label for="name">Name</label>
                <input type="text" id="name" required>
                <div class="step-buttons">
                    <button type="button" onclick="nextStep()">Next</button>
                </div>
            </div>
            <div class="step">
                <h2>Step 2</h2>
                <label for="email">Email</label>
                <input type="email" id="email" required>
                <div class="step-buttons">
                    <button type="button" onclick="prevStep()">Back</button>
                    <button type="button" onclick="nextStep()">Next</button>
                </div>
            </div>
            <div class="step">
                <h2>Step 3</h2>
                <label for="age">Age</label>
                <input type="number" id="age" required>
                <div class="step-buttons">
                    <button type="button" onclick="prevStep()">Back</button>
                    <button type="submit">Submit</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        let currentStep = 0;

        function showStep(step) {
            const steps = document.querySelectorAll('.step');
            steps.forEach((stepEl, index) => {
                stepEl.classList.toggle('active', index === step);
            });
        }

        function nextStep() {
            currentStep++;
            if (currentStep >= document.querySelectorAll('.step').length) {
                currentStep = document.querySelectorAll('.step').length - 1;
            }
            showStep(currentStep);
        }

        function prevStep() {
            currentStep--;
            if (currentStep < 0) {
                currentStep = 0;
            }
            showStep(currentStep);
        }
    </script>
</body>
</html>

举报

相关推荐

0 条评论