[JS] 체크된 radio 값 가져오기

happypath·2021년 11월 24일
0

Web

목록 보기
5/6

아래처럼 묶인 별점 체크 input에서 체크된 별에 해당하는 값을 가져오고 싶었다.

//별점 체크
<fieldset id='star-rating' class='rating'>
   <input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="정말 최고에요!"></label>
   <input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="좋아요"></label>
   <input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="괜찮아요"></label>
   <input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="아쉬웠어요"></label>
   <input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="별로에요"></label>
</fieldset>

'name'을 모두 같은 이름으로 지정해 주고,
복수의 radio 버튼을 묶은 fieldset에 id를 지정해 준뒤,
아래와 같은 jquery를 실행하면!
체크된 값에 해당하는 value가 예쁘게 들어온다.

let star_rating =$("#star-rating input[type=radio]:checked").val()

0개의 댓글