데일리 공부 기록
hands on vue3 - 제공된 소스로 체크,라디오,드랍박스 값을 v-model로 구해보자
탐훈
2023. 3. 16. 23:51
728x90
[목표]
: v-model로 name으로 grouping된 체크, 라디오, 드랍박스 값을 구하여
버튼을 클릭하여 submit할 때 console에 나오게 하라
해답을 보기 전 제공된 소스로 직접 해보길 바람..
[App.vue]
<template>
<the-form></the-form>
</template>
<script>
import TheForm from './components/TheForm.vue';
export default {
components: {
TheForm
}
}
</script>
<style>
* {
box-sizing: border-box;
}
html {
font-family: sans-serif;
}
body {
margin: 0;
background-color: #292929;
}
</style>
[TheForm.vue]
<template>
<form @submit.prevent="submitData">
<div class="form-control">
<label for="user-name">Your Name</label>
<input id="user-name" name="user-name" type="text" v-model="inputName"/>
</div>
<div class="form-control">
<label for="age">Your Age (Years)</label>
<input id="age" name="age" type="number" v-model.="inputAge" ref="userAge"/>
</div>
<div class="form-control">
<label for="referrer">How did you hear about us?</label>
<select id="referrer" name="referrer">
<option value="google">Google</option>
<option value="wom">Word of mouth</option>
<option value="newspaper">Newspaper</option>
</select>
</div>
<div class="form-control">
<h2>What are you interested in?</h2>
<div>
<input id="interest-news" name="interest" type="checkbox" />
<label for="interest-news">News</label>
</div>
<div>
<input id="interest-tutorials" name="interest" type="checkbox" />
<label for="interest-tutorials">Tutorials</label>
</div>
<div>
<input id="interest-nothing" name="interest" type="checkbox" />
<label for="interest-nothing">Nothing</label>
</div>
</div>
<div class="form-control">
<h2>How do you learn?</h2>
<div>
<input id="how-video" name="how" type="radio" />
<label for="how-video">Video Courses</label>
</div>
<div>
<input id="how-blogs" name="how" type="radio" />
<label for="how-blogs">Blogs</label>
</div>
<div>
<input id="how-other" name="how" type="radio" />
<label for="how-other">Other</label>
</div>
</div>
<div>
<button>Save Data</button>
</div>
</form>
</template>
<script>
export default{
data(){
return{
inputName: '',
inputAge: 0,
};
},
methods: {
submitData(){
console.log(this.inputName);
console.log(this.inputAge + 10);
console.log(this.$refs.userAge.value + 10);
this.inputName = '';
}
}
}
</script>
<style scoped>
form {
margin: 2rem auto;
max-width: 40rem;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
padding: 2rem;
background-color: #ffffff;
}
.form-control {
margin: 0.5rem 0;
}
label {
font-weight: bold;
}
h2 {
font-size: 1rem;
margin: 0.5rem 0;
}
input,
select {
display: block;
width: 100%;
font: inherit;
margin-top: 0.5rem;
}
select {
width: auto;
}
input[type='checkbox'],
input[type='radio'] {
display: inline-block;
width: auto;
margin-right: 1rem;
}
input[type='checkbox'] + label,
input[type='radio'] + label {
font-weight: normal;
}
button {
font: inherit;
border: 1px solid #0076bb;
background-color: #0076bb;
color: white;
cursor: pointer;
padding: 0.75rem 2rem;
border-radius: 30px;
}
button:hover,
button:active {
border-color: #002350;
background-color: #002350;
}
</style>
아래는 해답이다
[App.vue]
수정없음
[TheForm.vue]
<template>
<form @submit.prevent="submitData">
<div class="form-control">
<label for="user-name">Your Name</label>
<input id="user-name" name="user-name" type="text" v-model="inputName"/>
</div>
<div class="form-control">
<label for="age">Your Age (Years)</label>
<input id="age" name="age" type="number" v-model="inputAge" ref="userAge"/>
</div>
<div class="form-control">
<label for="referrer">How did you hear about us?</label>
<select id="referrer" name="referrer">
<option value="google">Google</option>
<option value="wom">Word of mouth</option>
<option value="newspaper">Newspaper</option>
</select>
</div>
<div class="form-control">
<h2>What are you interested in?</h2>
<div>
<input id="interest-news" name="interest" type="checkbox" value="News" v-model="interst" />
<label for="interest-news">News</label>
</div>
<div>
<input id="interest-tutorials" name="interest" type="checkbox" value="Tutorials" v-model="interst" />
<label for="interest-tutorials">Tutorials</label>
</div>
<div>
<input id="interest-nothing" name="interest" type="checkbox" value="Nothing" v-model="interest"/>
<label for="interest-nothing">Nothing</label>
</div>
</div>
<div class="form-control">
<h2>How do you learn?</h2>
<div>
<input id="how-video" name="how" type="radio" value="Video" v-model="how" />
<label for="how-video">Video Courses</label>
</div>
<div>
<input id="how-blogs" name="how" type="radio" value="Blogs" v-model="how"/>
<label for="how-blogs">Blogs</label>
</div>
<div>
<input id="how-other" name="how" type="radio" value="Other" v-model="how" />
<label for="how-other">Other</label>
</div>
</div>
<div>
<input type="checkbox" v-model="agreement">
<p>Do you agree with that we can use your private information?</p>
</div>
<div>
<button>Save Data</button>
</div>
</form>
</template>
<script>
export default{
data(){
return{
inputName: '',
inputAge: 0,
interest: [],
how:'',
agreement: null,
};
},
methods: {
submitData(){
console.log(this.inputName);
console.log(this.inputAge + 10);
console.log(this.$refs.userAge.value + 10);
console.log(this.interest);
console.log(this.how);
console.log(this.agreement);
this.inputAge = '';
this.interest = '';
this.how = '';
this.inputName = '';
}
}
}
</script>
<style scoped>
form {
margin: 2rem auto;
max-width: 40rem;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
padding: 2rem;
background-color: #ffffff;
}
.form-control {
margin: 0.5rem 0;
}
label {
font-weight: bold;
}
h2 {
font-size: 1rem;
margin: 0.5rem 0;
}
input,
select {
display: block;
width: 100%;
font: inherit;
margin-top: 0.5rem;
}
select {
width: auto;
}
input[type='checkbox'],
input[type='radio'] {
display: inline-block;
width: auto;
margin-right: 1rem;
}
input[type='checkbox'] + label,
input[type='radio'] + label {
font-weight: normal;
}
button {
font: inherit;
border: 1px solid #0076bb;
background-color: #0076bb;
color: white;
cursor: pointer;
padding: 0.75rem 2rem;
border-radius: 30px;
}
button:hover,
button:active {
border-color: #002350;
background-color: #002350;
}
</style>
추가설명
checkbox는 v-model로 일일이 다 속성에 넣어줘야함
그리고 checkbox 태그 안에 value로 값을 넣어줘야함
왜냐하면 v-model 배열로 받기 때문임
뭐 나머지는...
다 이해하리라 믿는다... 넘졸리드아