_yoonie
개발일기
_yoonie
전체 방문자
오늘
어제
  • 분류 전체보기 (70)
    • 개발일기 (2)
    • DB (8)
      • MySQL (2)
      • PostgreSQL (6)
    • WEB (30)
      • JAVASCRIPT (7)
      • Vue.js (2)
      • MyBatis (3)
      • PHP (7)
      • HTML_CSS (5)
      • etc. (3)
    • Java (9)
    • Android (5)
    • Kotlin (0)
    • C# (3)
    • Python (2)
    • 개발 툴 (5)
    • 기획 (1)
    • 자료실 (2)
    • React (0)

인기 글

태그

  • PostgreSQL
  • Java
  • java programming
  • 기획
  • androidstudio
  • ubuntu
  • CSS
  • 안드로이드스튜디오
  • c#
  • VirtualBox

최근 댓글

최근 글

티스토리

160x600
hELLO · Designed By 정상우.
_yoonie

개발일기

[Vue] input checkbox value 'Y' 'N'으로 설정
WEB/Vue.js

[Vue] input checkbox value 'Y' 'N'으로 설정

2022. 9. 20. 23:01

# Vue.js input property 사용하기

checkbox type의 input태그의 기본 값은 true/false이다.
DB에서 true/false가 아닌 'Y'/'N'이나 'yes'/'no' 등으로 관리할 경우
input tag property인 true-value와 false-value를 사용한다.
v-model을 사용하여 기본 값을 넣어줘야 제대로 들어간다.

<input type="checkbox" v-model="data.isSecure" true-value="Y" false-value="N">

주의할 점은 form submit을 할 경우 체크되지 않은 값은 전송되지 않는다.
v-model로 바인딩하여 전송한다면 문제없다.

더보기

# 사용 예

...
  <input type="checkbox" v-model="item.isSecure"  true-value="Y" false-value="N">
...
<script>
    window.app = new Vue({
        el: '#app',
        data: {
            item: {
                isSecure: 'N',
            }
        },
        methods: {
            writeBoard() {
                axios.post("URL", this.item, {
          		...

 

# change event 사용해서 직접 value 넣어주기

file type input에는 v-model을 사용하지 못하기 때문에 form에 type이 file인 input이 있다면 문제가 된다.
아래의 방법은 form submit으로도 전송할 수 있다.

<input class="custom-control-input" type="checkbox" id="isSecure" name="isSecure" @change="check">

...
check(e){
  if(e.target.checked){
       e.target.value = 'Y';
  }else{
       e.target.value = 'N';
  }
}

 

728x90
저작자표시 (새창열림)

'WEB > Vue.js' 카테고리의 다른 글

[Vue2] 오류: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated:  (0) 2023.06.12
    _yoonie
    _yoonie
    일상이 개발인 개발자의 일상

    티스토리툴바