๊ฐœ๋ฐœ๊ด€๋ จ/javascript&jquery

[Jquery/Javascript] ์ฒดํฌ๋ฐ•์Šค ์ „์ฒด ์„ ํƒ๊ณผ ์ „์ฒด ํ•ด์ œ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿป๊ณฐ์ด๐Ÿป 2022. 8. 2.
728x90

์•ˆ๋…•ํ•˜์„ธ์š” ๊ฐœ๋ฐœ์‰ฝ๊ฒŒํ•˜์ž๊ณฐ ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋žœ๋งŒ์— ๊ฐœ๋ฐœ ๊ด€๋ จ ํฌ์ŠคํŒ… ์จ๋ณด๋„ค์š”!

 

์˜ค๋Š˜์€ jquery๋กœ ์ฒดํฌ๋ฐ•์Šค ์ „์ฒด ์„ ํƒ ๊ด€๋ จํ•ด์„œ ์จ๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค.

 

์šฐ์„  ์…€๋ ‰ํ„ฐ๋ฅผ input์— name์œผ๋กœ ์žก์•˜์„ ๋•Œ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•

 

prop ๋ฐฉ์‹

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="Generator" content="EditPlus®">
	<meta name="Author" content="">
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<title>easy-developmentbear</title>
	<!-- ์ œ์ด์ฟผ๋ฆฌ -->
	<script src="https://code.jquery.com/jquery-latest.min.js"></script>
	<script>
		//์ „์ฒด์„ ํƒ ํ•จ์ˆ˜
		function all_check()
		{
			$("input[name='check']").prop("checked", true);
		}
	</script>
</head>

<body>
	<h2>์˜ค๋Š˜์˜ ์ ์‹ฌ</h2>
	<button onclick="all_check();">์ „์ฒด์„ ํƒ</button>
	<input type="checkbox" name="check" id="check_id" value="1">๊ตญ๋ฐฅ
	<input type="checkbox" name="check" id="check_id" value="1">ํ–„๋ฒ„๊ฑฐ
	<input type="checkbox" name="check" id="check_id" value="1">์นผ๊ตญ์ˆ˜
	<input type="checkbox" name="check" id="check_id" value="1">์ค‘์‹
	<input type="checkbox" name="check" id="check_id" value="1">์ดˆ๋ฐฅ
	<input type="checkbox" name="check" id="check_id" value="1">ํŒŒ์Šคํƒ€
	<input type="checkbox" name="check" id="check_id" value="1">ํ”ผ์ž
</body>
</html>

 

 

easy-developmentbear

์˜ค๋Š˜์˜ ์ ์‹ฌ

๊ตญ๋ฐฅ ํ–„๋ฒ„๊ฑฐ ์นผ๊ตญ์ˆ˜ ์ค‘์‹ ์ดˆ๋ฐฅ ํŒŒ์Šคํƒ€ ํ”ผ์ž

 

 

 

์ „์ฒด ์„ ํƒ ์‹œ ์ด๋ ‡๊ฒŒ ์ „์ฒด์ ์œผ๋กœ ์„ ํƒ๋˜๋Š” ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

 

 

attr ๋ฐฉ์‹

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="Generator" content="EditPlus®">
	<meta name="Author" content="">
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<title>easy-developmentbear</title>
	<!-- ์ œ์ด์ฟผ๋ฆฌ -->
	<script src="https://code.jquery.com/jquery-latest.min.js"></script>
	<script>
		//์ „์ฒด์„ ํƒ ํ•จ์ˆ˜
		function all_check2()
		{
			$("input[name='check1']").attr("checked", true);
		}
	</script>
</head>

<body>
	<h2>์˜ค๋Š˜์˜ ์ ์‹ฌ</h2>
	<button onclick="all_check2();">์ „์ฒด์„ ํƒ</button>
	<input type="checkbox" name="check1" id="check_id" value="1">๊ตญ๋ฐฅ
	<input type="checkbox" name="check1" id="check_id" value="1">ํ–„๋ฒ„๊ฑฐ
	<input type="checkbox" name="check1" id="check_id" value="1">์นผ๊ตญ์ˆ˜
	<input type="checkbox" name="check1" id="check_id" value="1">์ค‘์‹
	<input type="checkbox" name="check1" id="check_id" value="1">์ดˆ๋ฐฅ
	<input type="checkbox" name="check1" id="check_id" value="1">ํŒŒ์Šคํƒ€
	<input type="checkbox" name="check1" id="check_id" value="1">ํ”ผ์ž
</body>
</html>

 

easy-developmentbear

์˜ค๋Š˜์˜ ์ ์‹ฌ

๊ตญ๋ฐฅ ํ–„๋ฒ„๊ฑฐ ์นผ๊ตญ์ˆ˜ ์ค‘์‹ ์ดˆ๋ฐฅ ํŒŒ์Šคํƒ€ ํ”ผ์ž

prop ๋ง๊ณ ๋„ ์ด๋ ‡๊ฒŒ attr๋กœ๋„ ์ „์ฒด ์„ ํƒ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

on(), click() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•.

 

click() ํ•จ์ˆ˜ ์ด์šฉ

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="Generator" content="EditPlus®">
	<meta name="Author" content="">
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<title>easy-developmentbear</title>
	<!-- ์ œ์ด์ฟผ๋ฆฌ -->
	<script src="https://code.jquery.com/jquery-latest.min.js"></script>
	<script>
		$(function(){
			$("#all_chk").click(function(){
				$("input[name='check2']").prop("checked", true);
			});
		});
	</script>
</head>

<body>
	<h2>์˜ค๋Š˜์˜ ์ ์‹ฌ</h2>
	<button id="all_chk">์ „์ฒด์„ ํƒ</button>
	<input type="checkbox" name="check2" id="check_id" value="1">๊ตญ๋ฐฅ
	<input type="checkbox" name="check2" id="check_id" value="1">ํ–„๋ฒ„๊ฑฐ
	<input type="checkbox" name="check2" id="check_id" value="1">์นผ๊ตญ์ˆ˜
	<input type="checkbox" name="check2" id="check_id" value="1">์ค‘์‹
	<input type="checkbox" name="check2" id="check_id" value="1">์ดˆ๋ฐฅ
	<input type="checkbox" name="check2" id="check_id" value="1">ํŒŒ์Šคํƒ€
	<input type="checkbox" name="check2" id="check_id" value="1">ํ”ผ์ž
</body>
</html>
easy-developmentbear

์˜ค๋Š˜์˜ ์ ์‹ฌ

๊ตญ๋ฐฅ ํ–„๋ฒ„๊ฑฐ ์นผ๊ตญ์ˆ˜ ์ค‘์‹ ์ดˆ๋ฐฅ ํŒŒ์Šคํƒ€ ํ”ผ์ž

 

on() ํ•จ์ˆ˜ ์ด์šฉ

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="Generator" content="EditPlus®">
	<meta name="Author" content="">
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<title>easy-developmentbear</title>
	<!-- ์ œ์ด์ฟผ๋ฆฌ -->
	<script src="https://code.jquery.com/jquery-latest.min.js"></script>
	<script>
		$(function(){
			$("#all_chk3").on('click',function(){
				$("input[name='check3']").prop("checked", true);
			});
		});
	</script>
</head>

<body>
	<h2>์˜ค๋Š˜์˜ ์ ์‹ฌ</h2>
	<button id="all_chk3">์ „์ฒด์„ ํƒ</button>
	<input type="checkbox" name="check3" id="check_id" value="1">๊ตญ๋ฐฅ
	<input type="checkbox" name="check3" id="check_id" value="1">ํ–„๋ฒ„๊ฑฐ
	<input type="checkbox" name="check3" id="check_id" value="1">์นผ๊ตญ์ˆ˜
	<input type="checkbox" name="check3" id="check_id" value="1">์ค‘์‹
	<input type="checkbox" name="check3" id="check_id" value="1">์ดˆ๋ฐฅ
	<input type="checkbox" name="check3" id="check_id" value="1">ํŒŒ์Šคํƒ€
	<input type="checkbox" name="check3" id="check_id" value="1">ํ”ผ์ž
</body>
</html>
easy-developmentbear

์˜ค๋Š˜์˜ ์ ์‹ฌ

๊ตญ๋ฐฅ ํ–„๋ฒ„๊ฑฐ ์นผ๊ตญ์ˆ˜ ์ค‘์‹ ์ดˆ๋ฐฅ ํŒŒ์Šคํƒ€ ํ”ผ์ž

 

์ด๋ ‡๊ฒŒ onclick ์˜ต์…˜์„ ์ง์ ‘ ์•ˆ์ฃผ๊ณ  ๋‚ด์žฅ ํ•จ์ˆ˜๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ „์ฒด ์„ ํƒ๊ณผ ์ „์ฒด ์„ ํƒ ํ•ด์ œ

๋งŽ์ด ์•Œ๋ ค์ง„  prop์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="Generator" content="EditPlus®">
	<meta name="Author" content="">
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<title>easy-developmentbear</title>
	<!-- ์ œ์ด์ฟผ๋ฆฌ -->
	<script src="https://code.jquery.com/jquery-latest.min.js"></script>
	<script>
		$(function(){
			$("#all_chk2").on('click',function(){
				//์ฒดํฌ๋ฐ•์Šค check์ƒํƒœ๊ฐ€ true๋ผ๋ฉด
				if($("input[name='check4']").prop("checked"))
				{
					//name์ด check4 ์ธ ์ฒดํฌ๋ฐ•์Šค ํ•ด์ œ
					$("input[name='check4']").prop("checked", false);
				}
				else
				{
					//name์ด check4 ์ธ ์ฒดํฌ๋ฐ•์Šค ์ฒดํฌ
					$("input[name='check4']").prop("checked", true);
				}
			});
		});
	</script>
</head>

<body>
	<h2>์˜ค๋Š˜์˜ ์ ์‹ฌ</h2>
	<button id="all_chk2">์ „์ฒด์„ ํƒ</button>
	<input type="checkbox" name="check4" id="check_id" value="1">๊ตญ๋ฐฅ
	<input type="checkbox" name="check4" id="check_id" value="1">ํ–„๋ฒ„๊ฑฐ
	<input type="checkbox" name="check4" id="check_id" value="1">์นผ๊ตญ์ˆ˜
	<input type="checkbox" name="check4" id="check_id" value="1">์ค‘์‹
	<input type="checkbox" name="check4" id="check_id" value="1">์ดˆ๋ฐฅ
	<input type="checkbox" name="check4" id="check_id" value="1">ํŒŒ์Šคํƒ€
	<input type="checkbox" name="check4" id="check_id" value="1">ํ”ผ์ž
</body>
</html>
easy-developmentbear

์˜ค๋Š˜์˜ ์ ์‹ฌ

๊ตญ๋ฐฅ ํ–„๋ฒ„๊ฑฐ ์นผ๊ตญ์ˆ˜ ์ค‘์‹ ์ดˆ๋ฐฅ ํŒŒ์Šคํƒ€ ํ”ผ์ž

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š” ใ…Žใ…Ž

 

ํฌ์ŠคํŒ… ์ž‘์„ฑํ•˜๋ฉด์„œ ๋Š๋‚€ ๊ฑด๋ฐ ๊ฐ์ž name๊ฐ’๊ณผ id๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ํ•ด์•ผ ๊ฒน์น˜์ง€ ์•Š๋”๋ผ๊ณ ์š” ใ…Žใ…Ž ์ƒˆ๋กญ๊ฒŒ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค.

 

๋ณต์‚ฌํ•˜์‹ค ๋ถ„๋“ค์€ name๊ฐ’ ํ™•์ธํ•˜์‹œ๊ณ  ์‚ฌ์šฉํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค ใ…Žใ…Ž

 

๋‹ค์Œ์— ๋” ์ข‹์€ ์ •๋ณด๋กœ ๋Œ์•„์˜ฌ๊ฒŒ์š” ๋๊นŒ์ง€ ๋ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€