+
105
-

回答

这个

<!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>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1-dev.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }
        .form-container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            max-width: 400px;
            margin: auto;
        }
        h2 {
            text-align: center;
            color: #333;
        }
        label {
            display: block;
            margin: 10px 0 5px;
            color: #555;
        }
        input {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            width: 38%;
            padding: 10px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            margin: 5px 1%;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>

<div id="app" class="form-container">
    <div v-if="currentStep < formConfig.length">
        <h2>Step {{ currentStep + 1 }}</h2>
        <form @submit.prevent="nextStep">
            <div v-for="field in formConfig[currentStep].fields" :key="field.model">
                <label>{{ field.label }}</label>
                <input
                    v-model="formData[field.model]"
                    :type="field.type"
                    required
                />
            </div>
            <button type="button" @click="prevStep" v-if="currentStep > 0">Previous</button>
            <button type="submit">Next</button>
        </form>
    </div>
    <div v-else>
        <h2>Form Submitted!</h2>
        <pre>{{ formData }}</pre>
        <button @click="resetForm">Reset</button>
    </div>
</div>

<script>
new Vue({
    el: '#app',
    data: {
        currentStep: 0,
        formData: {},
        formConfig: [
            {
                step: 1,
                fields: [
                    { label: "Name", type: "text", model: "name" },
                    { label: "Email", type: "email", model: "email" }
                ]
            },
            {
                step: 2,
                fields: [
                    { label: "Address", type: "text", model: "address" },
                    { label: "Phone", type: "tel", model: "phone" }
                ]
            }
        ]
    },
    methods: {
        nextStep() {
            this.currentStep++;
        },
        prevStep() {
            this.currentStep--;
        },
        resetForm() {
            this.currentStep = 0;
            this.formData = {};
        }
    }
});
</script>

</body>
</html>

网友回复

我知道答案,我要回答