[TIL] HTML | form, input ์ •๋ฆฌ

dosilvยท2021๋…„ 4์›” 19์ผ
0
post-thumbnail
post-custom-banner

๐ŸŽฒ ๋ชฉํ‘œ: ์‚ฌ์šฉ์ž์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด ์ค‘์š”ํ•œ form&input ํƒœ๊ทธ ๋ฝ€๊ฐœ๊ธฐ~~

โ–ถ ๋ชฉ์ฐจ

๐Ÿ”Ž form
๐Ÿ”Ž label
๐Ÿ”Ž button
๐Ÿ”Ž input
๐Ÿ”Ž select & option
๐Ÿ”Ž textarea

๋ญ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ํ—ท๊ฐˆ๋ฆฌ๋Š”๋ฐ ๐Ÿ”Ž๊ฐ€ ๋ถ™์€ ๊ฑด tag๊ณ , ๐Ÿ–๋กœ ํ‘œ์‹œ๋œ ๊ฑด attribute!

๐Ÿ”Ž form

ํ…Œ์ด๋ธ”๋กœ ์น˜๋ฉด <table>๊ฐ™์€ ์—ญํ• ! <table>์ด <tr>, <td>, <th>, <thead> ๋“ฑ์„ ๊ฐ์‹ธ๋“ฏ์ด <form>๋„ <input>, <button> ๋“ฑ์„ ๊ทธ๋ฃนํ•‘ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ ๋ฌถ๋Š” ๊ฒƒ์„ ๋„˜์–ด์„œ submit๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋””๋กœ ๋ณด๋‚ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค ์คŒ!

๐Ÿ– action="URL"

form์„ submitํ•˜๋ฉด HTTP request๋ฅผ ๋ณด๋‚ด๊ฒŒ ๋˜๊ณ , action ์†์„ฑ์„ ํ†ตํ•ด ํ•ด๋‹น ์š”์ฒญ์„ ์–ด๋””๋กœ ๋ณด๋‚ผ์ง€ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ. ๋ฐ์ดํ„ฐ๊ฐ€ ๋„์ฐฉํ•  URL.

๐Ÿ– autocomplete="on"/"off"

์ž๋™์™„์„ฑ๊ธฐ๋Šฅ. ์†์„ฑ๊ฐ’์„ on์œผ๋กœ ์ฃผ๋ฉด ์‚ฌ์šฉ์ž์˜ ๊ณผ๊ฑฐ ์ž…๋ ฅ๊ฐ’์— ๊ธฐ๋ฐ˜ํ•ด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ž๋™์œผ๋กœ ๊ฐ’์„ ์ฑ„์›Œ ๋„ฃ์Œ.

๐Ÿ– method="get","post" ๋“ฑ

์–‘์‹์„ submitํ•  ๋•Œ ์‚ฌ์šฉํ•  HTTP method

  • post: ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ ๋ณธ๋ฌธ์œผ๋กœ ์ „์†กํ•จ
  • get(defualt): ๋ฐ์ดํ„ฐ๋ฅผ [action์˜ url] + [?] ๋’ค์— ์ด์–ด๋ถ™์—ฌ ์ „์†กํ•จ

๐Ÿ– target="_blank", "_self" ๋“ฑ

sumbit ํ›„ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์‘๋‹ต์„ ์–ด๋–ป๊ฒŒ ํ‘œ์‹œํ• ์ง€ ๊ด€๋ฆฌํ•˜๋Š” ์†์„ฑ

  • _blank: ์ƒˆ ํƒญ์ด๋‚˜ ์œˆ๋„์šฐ๋กœ ์—ด๊ธฐ
  • _self(defualt): ๋งํฌ๊ฐ€ ์œ„์น˜ํ•œ ํ˜„์žฌ ํ”„๋ ˆ์ž„์—์„œ ์—ด๊ธฐ
  • _parent: ํ˜„์žฌ ํ”„๋ ˆ์ž„์˜ ๋ถ€๋ชจ ํ”„๋ ˆ์ž„์—์„œ ์—ด๊ธฐ


๐Ÿ”Ž label

  • ์–ด๋–ค text๊ฐ€ ์–ด๋–ค input์— ๋Œ€ํ•œ ๊ฒƒ์ธ์ง€ ์—ฐ๊ด€์‹œ์ผœ ์ฃผ๋Š” ํƒœ๊ทธ (label์˜ text๋ฅผ ํด๋ฆญํ•˜๋ฉด ์—ฐ๊ฒฐ๋œ input์ด ํ™œ์„ฑํ™”๋จ!)
  • ๊ด€๋ จ๋œ label์˜ for๊ณผ input์˜ id๊ฐ€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค
  • input ์•ž์— ์œ„์น˜ํ•ด๋„, ๋’ค์— ์œ„์น˜ํ•ด๋„ ์ƒ๊ด€์—†์Œ!
<label for="username">What's your name?</label>
<input type="text" name="username" id="username" placeholder="enter your name!">

<input type="checkbox" id="cheese"><label for="cheese"> I like cheese</label>

๊ฒฐ๊ณผ๐Ÿ”ป

What's your name?

I like cheese

๋ฒจ๋กœ๊ทธ ํฌ์ŠคํŒ…์—์„œ๋Š” label์˜ ํ…์ŠคํŠธ๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น input์ด ํ™œ์„ฑํ™”๋˜๋Š” ๊ธฐ๋Šฅ์ด ์•ˆ ๋จนํžŒ๋‹ค. ๐Ÿ™„



๐Ÿ”Ž button

form ํƒœ๊ทธ์˜ attribute ์†์„ฑ์— ๋”ฐ๋ผ submit๋จ

๐Ÿ– type="submit", "reset", "button"

  • submit(default): ์„œ๋ฒ„๋กœ ์–‘์‹ ๋ฐํ„ฐ๋ฅผ ์ œ์ถœํ•จ
  • reset: ๋ชจ๋“  ์ปจํŠธ๋กค์„ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ๋Œ๋ฆผ
  • button: ํด๋ฆญํ–ˆ์„ ๋•Œ ์•„๋ฌด๊ฒƒ๋„ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์Œ (๊ธฐ๋ณธํ–‰๋™์ด ์—†์Œ)

๐Ÿ– autofocus

ํŽ˜์ด์ง€ ๋กœ๋“œ ํ›„ ์ž๋™์œผ๋กœ ํ•ด๋‹น ๋ฒ„ํŠผ์— ํฌ์ปค์Šค๋ฅผ ์ฃผ๋Š” ์†์„ฑ (ํ•œ ํŽ˜์ด์ง€๋‹น ํ•˜๋‚˜์˜ ์š”์†Œ๋งŒ autofocus๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ!)

๐Ÿ– disabled

๋ˆ„๋ฅด๊ฑฐ๋‚˜ ํด๋ฆญํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์Œ (๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์ „์— submitํ•˜๋Š” ๊ฑธ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์„ ๋“ฏ!)



๐Ÿ”Ž input

form์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ input ํƒœ๊ทธ! type์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ž…๋ ฅ ์–‘์‹์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ํด๋กœ์ง• ํƒœ๊ทธ๊ฐ€ ์—†๋Š” ๊ฒŒ ํŠน์ง•. id, name, value ๋“ฑ์˜ ์†์„ฑ์€ ์‹ค์ œ๋กœ ํ™”๋ฉด์— ๊ตฌํ˜„๋˜์ง€๋Š” ์•Š์ง€๋งŒ ๋ฐ์ดํ„ฐ ์—ฐ๊ฒฐ์„ ์œ„ํ•ด ์ค‘์š”ํ•จ!

๐Ÿ– type

  • input์˜ ์–‘์‹์„ ์ง€์ •ํ•˜๋Š” ์†์„ฑ
  • ๋งŽ์ด ์“ฐ์ด๋Š” text, password, number๋ถ€ํ„ฐ checkbox, range, radio, color, date, weak, month ๋“ฑ...... ๋งค์šฐ๋งค์šฐ ๋‹ค์–‘

๐Ÿ– name

  • submitํ–ˆ์„ ๋•Œ action์œผ๋กœ ์ฃผ์–ด์ง„ url ๋’ค์— ๋ง๋ถ™์—ฌ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’
  • {attribute ์†์„ฑ๊ฐ’}?{name}={์ž…๋ ฅ๋ฐ›์€ data} ์˜ ํ˜•ํƒœ

๐Ÿ– id

label์˜ for ์†์„ฑ๊ฐ’๊ณผ ๋งค์น˜๋˜์–ด์•ผ ํ•˜๋Š” ์†์„ฑ

๐Ÿ– placeholder

  • text, password, number ๋“ฑ ์ง์ ‘ ํƒ€์ดํ•‘ ๊ฐ€๋Šฅํ•œ input ์š”์†Œ์—์„œ ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๋””ํดํŠธ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๊ฐ’
  • ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‚ฌ๋ผ์ง (value์™€์˜ ์ฐจ์ด)

๐Ÿ– value

  • text๋ฅผ ํƒ€์ดํ•‘ํ•ด์•ผ ํ•˜๋Š” input ์š”์†Œ์— value๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ, ํŽ˜์ด์ง€ ๋กœ๋“œ์‹œ value๊ฐ’์ด ์ž…๋ ฅ๋œ ์ƒํƒœ๋กœ ๋‚˜ํƒ€๋‚จ (์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ value๊ฐ’์„ ์ง€์›Œ์•ผ ํ•˜๋ฉฐ, ์ง€์šฐ๋ฉด placeholder๊ฐ’์ด ๋‚˜ํƒ€๋‚จ)
  • checkbox, radio ๋“ฑ์˜ input์— value๋ฅผ ์„ค์ •ํ•˜๋ฉด, submit๋˜์—ˆ์„ ๋•Œ name๊ณผ ํ•จ๊ป˜ value๊ฐ’์ด ์ „๋‹ฌ๋จ! (์„ค์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์„ ํƒ์—ฌ๋ถ€์— ๋”ฐ๋ผ on/off๋กœ ํ‘œ์‹œ)

๐Ÿ– required

์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ submitํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•˜๋Š” ์†์„ฑ

๐Ÿ– minlength/maxlength, min/max

์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์ด ์œ ํšจํ•œ ๊ฐ’์ด ๋˜๋„๋ก ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•˜๋Š” ์†์„ฑ๋“ค!
input ๊ฐ’์ด text ํ˜•์‹์ธ ๊ฒฝ์šฐ minlength/maxlength ์†์„ฑ์„ ํ†ตํ•ด ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ์ตœ์†Œ/์ตœ๋Œ€์˜ ๊ธ€์ž์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ input ๊ฐ’์ด number์ธ ๊ฒฝ์šฐ(number, date, range ๋“ฑ) min/max๋กœ ์ตœ์†Ÿ๊ฐ’/์ตœ๋Œ“๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ– pattern

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์ด ์œ ํšจํ•˜๋„๋ก ํ˜•ํƒœ๋ฅผ ์ง€์ •ํ•˜๋Š” ์†์„ฑ!
์ •๊ทœํ‘œํ˜„์‹(Regular Expression)์„ ํ†ตํ•ด์„œ ๋ฌธ์ž์™€ ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ๋“ฑ ํŒจํ„ด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.

๐Ÿ– ์•„๋ž˜๋ถ€ํ„ฐ๋Š” ๋‹ค์–‘ํ•œ input type๋“ค

๐Ÿ“‹ type="text"

<lable for="mood">How are you today?</lable>
<input type="text" id="mood" name="mood" placeholder="Today I am...">

๊ฒฐ๊ณผ๐Ÿ”ป
How are you today?


๐Ÿ“‹ type="number"

<lable for="age">How old are you?</lable>
<input type="number" id="age" name="age" placeholder="20" min="1" max="100">

๊ฒฐ๊ณผ๐Ÿ”ป
How old are you?


๐Ÿ“‹ type="radio"

  • checkbox์™€ ๋‹ค๋ฅธ ์ : radio์˜ ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ๊ฐœ์˜ input ์ค‘ ํ•œ ๊ฐ€์ง€๋งŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค
  • ๊ทธ๋ฃนํ™”ํ•  ๋ณด๊ธฐ๋“ค์€ ๋™์ผํ•œ name์„ ๊ฐ€์ ธ์•ผ ํ•จ!
<label for="xs">XS</label>
<input type="radio" name="size" id="xs" value="xs">
<label for="s">S</label>
<input type="radio" name="size" id="s" value="s">
<label for="m">M</label>
<input type="radio" name="size" id="m" value="m">

๊ฒฐ๊ณผ๐Ÿ”ป
XS
S
M

label์„ ๋ˆ„๋ฅด๋ฉด ์›๋ž˜ ์—ฐ๊ฒฐ๋œ radio input์ด ์„ ํƒ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ... ๋ฒจ๋กœ๊ทธ์—์„  ์ด๊ฒƒ๋„ ์•ˆ ๋จนํžŒ๋‹ค ๐Ÿ˜’ ๊ทธ๋ฆฌ๊ณ  ์›๋ž˜ ๊ฐ™์€ name์„ ๊ฐ€์ง„ input ์ค‘ ํ•˜๋‚˜๋งŒ ์„ ํƒ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„  ์ค‘๋ณต์„ ํƒ๋„ ๋˜๋Š” ๋“ฏ?? ๋จธ์•ผ ์ฐธ๋‚˜;;


๐Ÿ“‹ type="range"

<label for="volume">๐Ÿ”Š</label>
<input type="range" id="volume" name="volume" min="0" max="100">

๊ฒฐ๊ณผ๐Ÿ”ป
๐Ÿ”Š
range์—์„œ๋„ value ์†์„ฑ์œผ๋กœ ๋””ํดํŠธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ!



๐Ÿ”Ž select & option

  • ์•„๋ž˜๋กœ ํŽผ์ณ์ง€๋Š” ๋“œ๋กญ๋‹ค์šด ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํƒœ๊ทธ!
  • option์— selected ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•ด๋‹น ์˜ต์…˜์ด ๋””ํดํŠธ๋กœ ์„ ํƒ๋˜์–ด ๋‚˜ํƒ€๋‚จ
<select name='fruits'>
  <option value='' selected>-- ์„ ํƒ --</option>
  <option value='apple'>๋ ˆ๋ชฌ</option>
  <option value='banana'>๋”ธ๊ธฐ</option>
  <option value='lemon'>๋ธ”๋ฃจ๋ฒ ๋ฆฌ</option>
</select>


๐Ÿ”Ž textarea

์ผ๋ฐ˜ input์˜ text๋ณด๋‹ค ๋งŽ์€ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•ด์•ผํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„ ํƒœ๊ทธ!
rows, cols๋กœ ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ

<textarea rows="10" cols="50" placeholder="์ž…๋ ฅํ•˜์„ธ์š”!"></textarea>



๐Ÿ™‡โ€โ™€๏ธ์ฐธ๊ณ ํ•œ ์ž๋ฃŒ

์ฝ”๋”ฉ์˜ ์‹œ์ž‘, TCP School

profile
DevelOpErUN ์„ฑ์žฅ์ผ๊ธฐ๐ŸŒˆ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€