๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
1๏ธโƒฃ Web/๊ธฐํƒ€

[Web] Form Tag์™€ ์ด๋ฒคํŠธ, ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

by seolhee2750 2022. 9. 18.

์˜ค๋Š˜์€ Form control ์š”์†Œ๋“ค๊ณผ ์†์„ฑ, Form ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•œ๋‹ค.

front ๋‹จ์—์„œ ์„œ๋ฒ„์™€ ์†Œํ†ตํ•˜๋Š” ๊ณต๊ฐ„ !

 

๐Ÿ“Ž Form

Form์˜ ๊ฐœ๋… ๋ฐ ์—ญํ• 

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์šฉ
  • ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ์„œ๋ฒ„๋Š” HTML form์„ ์ „๋‹ฌ
  • ์‚ฌ์šฉ์ž๋Š” HTML form์— ์ ์ ˆํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ›„ ์„œ๋ฒ„๋กœ ์ „์†กํ•จ (submit)
  • ์„œ๋ฒ„๋Š” ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ ๋ถ„์„ ํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ๋“ฑ๋กํ•˜๊ฑฐ๋‚˜, ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•จ

๐Ÿ‘‰ Form์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ , ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ์–‘์‹

 

Form ์ž‘์„ฑ

<form [์†์„ฑ="์†์„ฑ๊ฐ’"]> form control </form>

๐Ÿ“Ž form control ์š”์†Œ

form control์ด๋ž€?

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” control ์š”์†Œ๋“ค์€ ๋ชจ๋‘ <form> ํƒœ๊ทธ์˜ ํ•˜์œ„์— ์œ„์น˜ํ•ด์•ผ๋งŒ ์„œ๋ฒ„๋กœ ์ „์†ก๋œ๋‹ค.
  • ๊ฐ control ์š”์†Œ๋งˆ๋‹ค ํ…์ŠคํŠธ ์ž…๋ ฅ, ๋ฒ„ํŠผ ํด๋ฆญ ๋“ฑ ๋‹ค์–‘ํ•œ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
tag ์„ค๋ช…
<form> ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ ๋ฐ›์„ ํ•ญ๋ชฉ์„ ์ •์˜, form tag ๋‚ด๋ถ€์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ control ์š”์†Œ๋ฅผ ํฌํ•จ
<input> ํ…์ŠคํŠธ box, ์ฒดํฌ box, ๋ผ๋””์˜ค ๋ฒ„ํŠผ ๋“ฑ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
<textarea> ์—ฌ๋Ÿฌ ์ค„์˜ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
<button> ๋ฒ„ํŠผ์„ ํ‘œ์‹œ
<select> select box(dropdown, combobox)๋ฅผ ํ‘œ์‹œ
<optgroup> select box์˜ ๊ฐ ํ•ญ๋ชฉ๋“ค์„ ๊ทธ๋ฃนํ™”
<label> ๋งˆ์šฐ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ <input> ํ•ญ๋ชฉ ์„ ํƒ ์‹œ ํŽธ๋ฆฌํ•จ์„ ์ œ๊ณต, for ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ control ์š”์†Œ์™€ ํ…์Šค๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์„œ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
<fieldset> ์ž…๋ ฅ ํ•ญ๋ชฉ๋“ค์„ ๊ทธ๋ฃนํ™”
<legend> <fieldset>์˜ ์ œ๋ชฉ์„ ์ง€์ •

๐Ÿ‘‰ <form> ํƒœ๊ทธ๋Š” ์ž…๋ ฅ๋ฐ›์„ ์š”์†Œ๋“ค์„ ๋‹ด๋Š” ํ•˜๋‚˜์˜ ์–‘์‹์„ ์˜๋ฏธ, ๋‚˜๋จธ์ง€ ํƒœ๊ทธ๋“ค์€ ๊ทธ ์–‘์‹์„ ๊ตฌ์„ฑํ•˜๋Š” ์ž…๋ ฅ ๋„๊ตฌ!


๐Ÿ“Ž ๋ฐ์ดํ„ฐ ์ „์†ก์„ ์œ„ํ•œ ์†์„ฑ

form์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ ํ•„์š”ํ•œ ์†์„ฑ๋“ค

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ž๋ฃŒ๋“ค์„ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •
  • ์„œ๋ฒ„์—์„œ ์–ด๋–ค ํ”„๋กœ๊ทธ๋žจ์„ ์ด์šฉํ•ด ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •
์†์„ฑ ์„ค๋ช…
method ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ์„œ๋ฒ„ ์ชฝ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋„˜๊ฒจ์ค„์ง€ ์ง€์ •ํ•จ
์†์„ฑ๊ฐ’ GET
์ฃผ์†Œ ํ‘œ์‹œ์ค„์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด ํ‘œ์‹œ๋˜๋ฉฐ, ์ „์†ก ๋‚ด์šฉ ๊ธธ์ด ์ œํ•œ์ด ์žˆ์Œ
POST
์ „์†ก ๋‚ด์šฉ ๊ธธ์ด์— ์ œํ•œ์ด ์—†์œผ๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด ํ‘œ์‹œ๋˜์ง€ ์•Š์Œ
name form์˜ ์ด๋ฆ„์„ ์ง€์ •. ํ•œ ๋ฌธ์„œ ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ <form> ํƒœ๊ทธ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•จ
action <form> ํƒœ๊ทธ ์•ˆ์˜ ๋‚ด์šฉ๋“ค์„ ์ฒ˜๋ฆฌํ•ด ์ค„ ์„œ๋ฒ„์ƒ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ง€์ •ํ•จ (URL)
target <action> ํƒœ๊ทธ์—์„œ ์ง€์ •ํ•œ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ํ˜„์žฌ ์ฐฝ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์œ„์น˜์— ์—ด๋„๋ก ์ง€์ •ํ•จ
autocomplete ์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ์œผ๋กœ, ๊ธฐ๋ณธ๊ฐ’์€ on

 

GET๊ณผ POST ์ฐจ์ด์ 

  • GET์€ HTTP ๋ฉ”์‹œ์ง€์˜ header ์˜์—ญ์— ๋‹ด์•„ ์ „์†กํ•˜๊ธฐ ๋•Œ๋ฌธ์—
    ์ „์†ก ๊ธธ์ด ์ œํ•œ(256~204bytes)์ด ์žˆ๊ณ , ์ฃผ์†Œ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.
  • POST๋Š” HTTP ๋ฉ”์‹œ์ง€์˜ body ์˜์—ญ์— ๋‹ด์•„ ์ „์†กํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€์ ์œผ๋กœ IO๊ฐ€ ์ผ์–ด๋‚œ๋‹ค.
    ๋”ฐ๋ผ์„œ ์ „์†ก ๋‚ด์šฉ์˜ ๊ธธ์ด์— ์ œํ•œ์ด ์—†๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์†Œ์— ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

์–ธ์ œ GET or POST๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

  • POST
    ํŽ˜์ด์ง€ ๋‹จ์œ„์˜ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ „์†ก, ํ˜น์€ ๋ณด์•ˆ์ด ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ „์†กํ•  ๋•Œ
  • GET
    ์งง์€ ์–‘์˜ ๋ฐ์ดํ„ฐ, ํ˜น์€ ๋…ธ์ถœ๋˜์–ด๋„ ์ƒ๊ด€์—†๋Š” ์ •๋ณด๋ฅผ ์ „์†กํ•  ๋•Œ
    (GET์˜ ๊ธธ์ด ์ œํ•œ์„ ์ดˆ๊ณผํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ „์†ก์ด ๋ถˆ๊ฐ€ํ•œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์งค๋ ค์„œ ์ „์†ก๋จ)

 

page ์ด๋™ ์‹œ GET๊ณผ POST ๊ตฌ๋ถ„ ๋ฐฉ๋ฒ•

page ์ด๋™์—๋Š” ์•„๋ž˜ ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  1. ์ฃผ์†Œ ํ‘œ์‹œ์ค„ ์ž…๋ ฅ
  2. aํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ link ์ด๋™
  3. form์„ ์ด์šฉํ•œ ์ด๋™ (GET, POST ๋ฐฉ์‹)

์—ฌ๊ธฐ์„œ 1, 2๋ฒˆ ๋ฐ 3๋ฒˆ์˜ GET ๋ฐฉ์‹ ์ „์†ก์€ ๋ชจ๋‘ GET์ด๊ณ ,

์œ ์ผํ•˜๊ฒŒ 3๋ฒˆ์˜ form์„ ์ด์šฉํ•œ POST ๋ฐฉ์‹ ์ „์†ก๋งŒ์ด POST์ด๋‹ค.

๐Ÿ‘‰ POST๋ผ๊ณ  ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด ๋ชจ๋‘ GET์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“Ž Form ์ž‘์„ฑ ์˜ˆ์‹œ

form ์˜ˆ์‹œ ํŽ˜์ด์ง€ ๋ฐ ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>ํšŒ์›๊ฐ€์ž…</title>
  </head>
  <body>
    <h1>ํšŒ์›๊ฐ€์ž…</h1>
    <form method="POST" action="register.jsp">
	    <fieldset>
	      <legend>์‚ฌ์šฉ์ž ์ •๋ณด</legend>
	      	<label for="id">์•„์ด๋”” </label>
	      		<input type="text" placeholder="ํ•„์ˆ˜ ์ž…๋ ฅ" id="id" required/></br>
	        <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ </label>
	        	<input type="password" placeholder="ํ•„์ˆ˜ ์ž…๋ ฅ" id="pass" required/></br>
	        <label>์„ฑ๋ณ„ </label>
	        	๋‚จ<input type="radio" name="sex"/>
	        	์—ฌ<input type="radio" name="sex"/></br>
	        <label>๊ด€์‹ฌ๋ถ„์•ผ </label>
	        	<input type="checkbox" name="sub"/>์˜ํ™” 
	        	<input type="checkbox" name="sub"/>์Œ์•… 
	        	<input type="checkbox" name="sub"/>๋ฏธ์ˆ  </br>
	        <label>์ƒํƒœ๋ฉ”์‹œ์ง€</label></br>
	        <textarea cols="30" rows="5"></textarea></br>
	        <input type="submit" value="๋“ฑ๋ก">
            <input type="reset" value="์ดˆ๊ธฐํ™”">
	    </fieldset>
    </form>
  </body>
</html>

๐Ÿ“Ž Form ์ด๋ฒคํŠธ

์ด๋ฒคํŠธ๋ž€?

  • ์›น ํŽ˜์ด์ง€์—์„œ ์—ฌ๋Ÿฌ ์ƒํ˜ธ์ž‘์šฉ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค ๋ฐœ์ƒ
  • ๋งˆ์šฐ์Šค ํด๋ฆญ ํ˜น์€ ํ‚ค๋ณด๋“œ๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์ด๋ฒคํŠธ ์กด์žฌ
    (์›น ํŽ˜์ด์ง€ ๋กœ๋”ฉ, ํŽ˜์ด์ง€ ์Šคํฌ๋กค, ๋ธŒ๋ผ์šฐ์ € ์ฐฝ ํฌ๊ธฐ ์กฐ์ ˆ, ๋งˆ์šฐ์Šค ํด๋ฆญ, Form submit ๋“ฑ)
  • JavaScript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DOM์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ ๊ฐ์ง€, ์ด๋ฒคํŠธ์— ๋Œ€์‘ํ•˜๋Š” ์—ฌ๋Ÿฌ ์ž‘์—… ์ˆ˜ํ–‰
  • ์ด๋ฒคํŠธ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜์™€ ์—ฐ๊ฒฐ, ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ํ•จ์ˆ˜ ์‹คํ–‰ (์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ˜น์€ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ผ๊ณ  ๋ถ€๋ฆ„)

 

Form ์ด๋ฒคํŠธ

  • form์ด ์ „์†ก๋  ๋•Œ๋Š” submit ์ด๋ฒคํŠธ, ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋Š” reset ์ด๋ฒคํŠธ ๋ฐœ์ƒ
  • submit๊ณผ reset์€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ ์ทจ์†Œํ•  ์ˆ˜๋„ ์žˆ์Œ
์ด๋ฒคํŠธ ์„ค๋ช…
onsubmit form์ด ์ „์†ก๋  ๋•Œ ๋ฐœ์ƒ
onreset ์ž…๋ ฅ form์ด reset๋  ๋•Œ ๋ฐœ์ƒ
oninput input ๋˜๋Š” textarea์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๋ฐœ์ƒ
onchange select box, check box, radio button์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๋ฐœ์ƒ
onfocus(focusin) input๊ณผ ๊ฐ™์€ ์š”์†Œ์— ์ž…๋ ฅ ํฌ์ปค์Šค๊ฐ€ ๋“ค์–ด์˜ฌ ๋•Œ ๋ฐœ์ƒ
onblur(focusout) input๊ณผ ๊ฐ™์€ ์š”์†Œ ๋“ฑ์—์„œ ํฌ์ปค์Šค๊ฐ€ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๋ฐœ์ƒ
onselect input, textarea์— ์ž…๋ ฅ ๊ฐ’ ์ค‘ ์ผ๋ถ€๊ฐ€ ๋งˆ์šฐ์Šค ๋“ฑ์œผ๋กœ ์„ ํƒ๋  ๋•Œ ๋ฐœ์ƒ

 

Form ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๊ตฌํ˜„

1๏ธโƒฃ ์ธ๋ผ์ธ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋ฐฉ์‹

<form method="POST" action="ok.html" onsubmit="javascript:event.preventDefault();alert('์•ˆ๋…•');">
  • ์œ„์—์„œ ๊ตฌํ˜„ํ–ˆ๋˜ ์˜ˆ์‹œ์˜ form ํƒœ๊ทธ ์•ˆ์—์„œ onsubmit ์†์„ฑ์„ ํ™œ์šฉํ–ˆ๋‹ค.
  • onsubmit ์•ˆ์— JavaScript๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿ‘‰ event.preventDefault()

submit๋˜๋Š” ์ˆœ๊ฐ„ ํŽ˜์ด์ง€๊ฐ€ ๋ฆฌ๋กœ๋“œ ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ!

 

2๏ธโƒฃ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ ๋ฐฉ์‹

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>ํšŒ์›๊ฐ€์ž…</title>
  </head>
  <body>
    <h1>ํšŒ์›๊ฐ€์ž…</h1>
    <form method="POST" action="ok.html" id="register">
	    <fieldset>
	      <legend>์‚ฌ์šฉ์ž ์ •๋ณด</legend>
	      	<label for="id" onsubmit="javascript:alert('id');">์•„์ด๋”” </label>
	      		<input type="text" placeholder="ํ•„์ˆ˜ ์ž…๋ ฅ" id="id" required/></br>
	        <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ </label>
	        	<input type="password" placeholder="ํ•„์ˆ˜ ์ž…๋ ฅ" id="pass" required/></br>
	        <label>์„ฑ๋ณ„ </label>
	        	๋‚จ<input type="radio" name="sex"/>
	        	์—ฌ<input type="radio" name="sex"/></br>
	        <label>๊ด€์‹ฌ๋ถ„์•ผ </label>
	        	<input type="checkbox" name="sub"/>์˜ํ™” 
	        	<input type="checkbox" name="sub"/>์Œ์•… 
	        	<input type="checkbox" name="sub"/>๋ฏธ์ˆ  </br>
	        <label>์ƒํƒœ๋ฉ”์‹œ์ง€</label></br>
	        <textarea cols="30" rows="5"></textarea></br>
	        <input type="submit" value="๋“ฑ๋ก">
	        <input type="reset" value="์ดˆ๊ธฐํ™”">
	    </fieldset>
    </form>
  </body>
  <script>
  	var f = document.getElementById("register")
  	f.onsubmit = function submitFunc() {
  		if(document.getElementById('id').value != "seolhee") {
  			alert("id๋ฅผ 'seolhee'๋ผ๊ณ  ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
  	  		return false;
  		}
  	}
  </script>
</html>
  • form์— id๋ฅผ ์ง€์ •ํ•˜๊ณ , HTML์—์„œ id๊ฐ’์„ ํ†ตํ•ด ์ด๋ฒคํŠธ ๋Œ€์ƒ์ด ๋  DOM์„ ์„ ํƒํ–ˆ๋‹ค.
  • ํ•ด๋‹น DOM์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ(submitFunc())๋ฅผ ๋“ฑ๋กํ•˜๊ณ , onsubmit์ด ๋ฐœ์ƒํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ–ˆ๋‹ค.

 

3๏ธโƒฃ addEventListener ๋ฉ”์†Œ๋“œ ๋ฐฉ์‹

<script>
  	var f = document.getElementById("register")
  	function submitFunc1() {
  		if(document.getElementById('id').value != "seolhee") {
  			alert("id๋ฅผ 'seolhee'๋ผ๊ณ  ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
  	  		return false;
  		}
  	}
  	function submitFunc2() {
  		if(document.getElementById('id').value != "seolhee") {
  			alert("id๋ฅผ 'seolhee2'๋ผ๊ณ  ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
  	  		return false;
  		}
  	}
    	// document.addEventListener(์ด๋ฒคํŠธ, ๋ฉ”์†Œ๋“œ, ์บก์ณ๋ง ์—ฌ๋ถ€)
  	f.addEventListener("submit", submitFunc1, false); // ๋งˆ์ง€๋ง‰ ์ธ์ž๊ฐ’ ์ƒ๋žต ๊ฐ€๋Šฅ
  	f.addEventListener("submit", submitFunc2, false); // ๋งˆ์ง€๋ง‰ ์ธ์ž๊ฐ’ ์ƒ๋žต ๊ฐ€๋Šฅ
  </script>
  • ๋‚˜๋จธ์ง€๋Š” ์œ„์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ ๋ฐฉ์‹๊ณผ ๊ฐ™๊ณ , JavaScript ์ฝ”๋“œ ๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝํ–ˆ๋‹ค.
  • addEventListener์˜ ํŠน์„ฑ์„ ๋‹ด๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ์— ๋‘ ๊ฐœ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.
  • id๋ฅผ ํ†ตํ•ด DOM์„ ์„ ํƒํ•˜๊ณ , ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํ•ด๋‹น DOM์— ๊ฐ๊ฐ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

 

โœ”๏ธ ์–ด๋–ค ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์žฅ์  ๋‹จ์ 
์ธ๋ผ์ธ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฉ”์†Œ๋“œ ํ•œ ๋ฒˆ์— ํ˜ธ์ถœ ๊ฐ€๋Šฅ HTML ์ฝ”๋“œ๋ฅผ JavaScript๊ฐ€ ์นจ๋ฒ”
์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ HTML ์ฝ”๋“œ์™€ JavaScript ์ฝ”๋“œ ๋ถ„๋ฆฌ ๊ฐ€๋Šฅ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ์—
ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋งŒ์„ ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ์Œ
(๋งˆ์ง€๋ง‰์— ๋ฐ”์ธ๋”ฉ๋œ ๋ฉ”์†Œ๋“œ๋งŒ ์‹คํ–‰๋จ)
addEventListener - addEventListener์˜ ์ธ์ž๋ฅผ ํ†ตํ•ด ์ข€ ๋” ์„ธ๋ฐ€ํ•œ ์ด๋ฒคํŠธ ์ œ์–ด ๊ฐ€๋Šฅ
- ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ํ•˜๋‚˜ ์ด์ƒ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
- ์บก์ณ๋ง๊ณผ ๋ฒ„๋ธ”๋ง ์ง€์›
- HTML ์š”์†Œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋“  DOM(HTML, XML, SVG)์— ๋Œ€ํ•ด ๋™์ž‘ํ•จ
๐Ÿ‘‰ ์žฅ์  ๅคš, ์ธ๋ผ์ธ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์™€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ์˜ ๋‹จ์  ๊ทน๋ณต

๐Ÿ‘‰ addEventListener๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌ


๐ŸŒŸ ์ •๋ฆฌ

form ํƒœ๊ทธ๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

submit์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ณ , ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋Š” addEventListener๋ฅผ ํ™œ์šฉํ•˜์ž.

๋Œ“๊ธ€