본문 바로가기
HTML

38일차//JQuery/radio, prop 사용시 체크박스 체크된다

by aesup 2021. 3. 8.
728x90
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src = "https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>


<!-- radio -->
<ul>
	<li><input type = "radio" name = "radio_test" value = "사과" checked>사과</li>
	
	<li><input type = "radio" name = "radio_test" value = "배">배</li>
	
	<li><input type = "radio" name = "radio_test" value = "바나나">바나나</li>
	

</ul>

	<button type="button" id = "chioce">선택</button>

<script type="text/javascript">
$(document).ready(function () {
	
	$("#chioce").click(function () {
		//getter
		/* var radioval = $("input[name='radio_test']:checked").val();
		alert(radioval); */
		//setter
		//var radioval = $("input[name='radio_test']").val(["바나나"]);
		/* alert(radioval); */
		//왜오류?
				
	});
	
});



</script>
<br><br>
<!-- /* checkbox */ -->

<input type="checkbox" id = "ch">그림그리기
<!-- 체크박스는 거의 아이디를 쓴다 -->

<br><br>
<button type="button" id = "btn">쳌크</button>

<script type="text/javascript">

$(function() {
	
	$("#btn").click(function() {
		
		//getter
		//let check = $("#ch").is(":checked");
		//alert("check:" + check);
		//아래와 위와 동일
		//let check = $("input:checkbox[id = 'ch']").is(":checked");
		//alert("check:" + check);
		
		
		//setter
		$("#ch").prop("checked", true);
		//ch 아이드를 가진 checkbox가 체크된다.
		//false시 체크가 되지않는다.
		
		
	});
	
	
});




</script>

</body>
</html>

728x90