IT/jQuery

jQuery - checkbox attr과 prop의 차이

노마드오브 2018. 11. 17. 23:51


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

$('button').click(function () {

// str은 문자열 타입

var str = $('input[type=checkbox]').attr('checked');

// val는 불리언 타입 true/false

var val = $('input[type=checkbox]').prop('checked');

alert(str + ' : ' + val);

});

});

</script>

</head>

<body>

<input type="radio" value="라디오버튼값" checked="checked">라디오버튼<br>

<input type="checkbox" value="체크박스값">체크박스<br>

<button type="button">버튼</button>

</body>

</html>