-
javascript) radio value 가져오기, 체크여부 확인카테고리 없음 2023. 11. 20. 17:18
-radio 체크된 값 가져오기
html)
<div id="radio_box">직급:<input class="input_position" type="radio" value="팀원" name="position_value">팀원<input class="input_position" type="radio" value="팀장" name="position_value">팀장</div>javascript)
var position = document.querySelector("input[name='position_value']:checked").valueconsole.log(position)ㄴdocument.querySelector("input[name='position_value']:checked").value
-radio 체크된 값 가져오기2, 체크여부 확인하기)
html)
<div id="radio_box">직급:<input class="input_position" type="radio" value="팀원" name="position_value">팀원<input class="input_position" type="radio" value="팀장" name="position_value">팀장</div>javascript)
var radioTag = document.getElementsByName("position_value");var position = null;
for(var i=0;i<radioTag.length;i++){ // 체크된 radio 찾기if(radioTag[i].checked == true){position=radioTag[i].value;break;}}
if(!!position){ // radio가 널값이 아니라면* 값 출력console.log("선택한값"+position)}else{ // radio가 널값이라면console.log("선택해주세요")}