[JS : 주의할점] 문자열, 숫자 구분

이지수·2021년 8월 31일
0
function schedule_insert(){
      var day =$("#day").val()-1;
      $($($(".todo-list").children()[day]).children()[1]).append("<div class ='parent'><div class = 'to_do'>" + $("#toDo").val()+ "</div><div><button type='button' class ='btn btn-outline-dark btn-sm m-1' onclick='edit(this)'>수정</button><button type='button' class ='btn btn-outline-dark btn-sm m-1' onclick='schedule_remove(this)'>삭제</button></div></div>");
    }

주간계획표 만든 코드에서 script태그 안을 보면 schedule_insert() 함수가 있는데 여기서

var day =$("#day").val()-1;

이부분이 사실 좋지 않은 코드이다.

왜냐면 아이디가 day인 태그는 input태그이고 input태그는 문자열을 받는다. 그런데 js에서 input태그의 value값에 -1을 해준것을 자의적으로 해석하여 숫자의 계산처럼 해준 것이다.

var day =$("#day").val()-1;

이 부분의 -1을 +1로 바꾸어 console.log를 찍어보니까 input창에 4를 넣었을 때 5가 아닌 41로 찍혔다.

문자열 숫자 구분!!! 주의 하자

profile
The only thing worse than starting something and failing...is not starting something

0개의 댓글