screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3.2.12/dist/vue.global.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <div class="container"> <h3>Vue js post checkbox data to API</h3> <form @submit="handleSubmit"> <div v-for="river in rivers" :key="river.id"> <label> <input type="checkbox" :value="river.name" :checked="selectedRivers.includes(river.name)" @change="handleCheckboxChange" /> {{ river.name }} </label> </div> <button type="submit">Submit</button> </form> </div> </div> <script type="module"> const app = Vue.createApp({ data() { return { selectedRivers: [], rivers: [ { id: 1, name: "Ganga" }, { id: 2, name: "Yamuna" }, { id: 3, name: "Brahmaputra" }, { id: 4, name: "Godavari" }, { id: 5, name: "Krishna" }, { id: 6, name: "Narmada" }, { id: 7, name: "Tapti" }, { id: 8, name: "Mahanadi" }, // Add more rivers as needed ], }; }, methods: { handleCheckboxChange(event) { const { value, checked } = event.target; if (checked) { this.selectedRivers.push(value); } else { this.selectedRivers = this.selectedRivers.filter( (river) => river !== value ); } }, handleSubmit(event) { event.preventDefault(); // Perform your post request with the selectedRivers data axios .post("/your-api-endpoint", { selectedRivers: this.selectedRivers, }) .then((response) => { console.log(response.data); alert("successfully submitttedF"); }) .catch((error) => { console.error(error); }); }, }, }); app.mount("#app"); </script> <style scoped> /* CSS for the container */ .container { max-width: 400px; margin: 0 auto; padding: 20px; background-color: #f4f4f4; border-radius: 10px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } /* CSS for the heading */ h3 { font-size: 24px; margin-bottom: 20px; } /* CSS for the form */ form { margin-bottom: 20px; } /* CSS for the checkbox container */ label { display: block; margin-bottom: 10px; } /* CSS for the checkbox */ input[type="checkbox"] { margin-right: 10px; } /* CSS for the submit button */ button[type="submit"] { display: block; width: 100%; padding: 10px; background-color: #4caf50; color: #fff; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; } /* CSS for the submit button on hover */ button[type="submit"]:hover { background-color: #45a049; } </style> </body> </html>