๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋ก ํŠธ์—”๋“œ/HTML

Form ํƒœ๊ทธ

by alswlfl 2022. 11. 18.

Form ํƒœ๊ทธ

Form ํƒœ๊ทธ

ํผ(Form): ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด ๋†“์€ ํ˜•์‹

ex) ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž…, ๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ๋“ฑ

ํผ: ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ๋“ค์˜ ๋ฌถ์Œ / ํผ ๋ฐ์ดํ„ฐ ๋˜๋Š” ํ•„๋“œ: ๋ฐ์ดํ„ฐ

 

<form> ์š”์†Œ๋Š” ์ •๋ณด๋ฅผ ์ œ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋””์„œ๋ถ€ํ„ฐ ์–ด๋””๊นŒ์ง€๊ฐ€ ์–‘์‹์ธ์ง€ ์ง€์ •ํ•˜๋Š” ์—ญํ• 

<form action="/signup" method="post">
	<div class="form-example">
		<label for="name">์ด๋ฆ„: <label>
		<input type="text" name="name" id="name" required>
	</div>
	<div class="form-example">
		<label for="email">์ด๋ฉ”์ผ: <email>
		<input type="email" name="email" id="email" required>
	</div>
	<div class="form-example">
		<input type="submit" value="์ œ์ถœํ•˜๊ธฐ">
	</div>
</form>

์†์„ฑ

  • action: ์–‘์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์˜ URI
  • method: ์–‘์‹์„ ์ œ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•  HTTP ๋ฉ”์„œ๋“œ
    • post: ์–‘์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ ๋ณธ๋ฌธ์œผ๋กœ ์ „์†ก
    • get: ์–‘์‹ ๋ฐ์ดํ„ฐ๋ฅผ URL์˜ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ ๋ถ™์—ฌ์„œ ์ „์†ก

 


Input ํƒœ๊ทธ

<input> ์š”์†Œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

text

<input> ํƒœ๊ทธ์˜ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ํ•œ ์ค„์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.

<input type="text" id="name">
  • email: email ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋จ (์ด๋ฉ”์ผ ์œ ํšจ์„ฑ ๊ฒ€์ฆ)
  • tel: ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ (๋ชจ๋ฐ”์ผ ์ ‘๊ทผ์‹œ ํ‚คํŒจ๋“œ ๋‹ค๋ฆ„)

label

<label> ๋ ˆ์ด๋ธ” ํƒœ๊ทธ๋Š” ์ž…๋ ฅ๋ฐ›๋Š” ํ•„๋“œ๋ฅผ ์„ค๋ช…ํ•  ๋•Œ ์‚ฌ์šฉ (์›น ์ ‘๊ทผ์„ฑ ์ค€์ˆ˜)

<!-- label ํƒœ๊ทธ ํ•˜์œ„์— ๋†“๋Š” ๋ฒ• -->
<label>
	์ด๋ฆ„:
	<input type="text" id="name">
</label>

<!-- for์™€ id์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ label ํƒœ๊ทธ์™€ ์—ฐ๊ฒฐ์ง€์Œ -->
<label for="name">์ด๋ฆ„ : </label>
<input type="text" id="name">

fieldset

์–‘์‹์˜ ์—ฌ๋Ÿฌ ์ปจํŠธ๋กค๊ณผ ๋ ˆ์ด๋ธ”(<label>)์„ ๋ฌถ์„ ๋•Œ ์‚ฌ์šฉ

 

legend

์š”์†Œ๋Š” ๋ถ€๋ชจ <fieldset>์ฝ˜ํ…์ธ ์˜ ์‹ค๋ช…์„ ๋‚˜ํƒ€๋ƒ„

<!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>Form Input</title>
  </head>
  <body>
    <form action="">
      <fieldset>
        <legend>Input Tag</legend>
        <ul>
          <li>
            <label for="text">Text</label>
            <input type="text" id="text" required value="weoifj" readonly />
          </li>
          <li>
            <label for="email">์ด๋ฉ”์ผ</label>
            <input type="email" id="email" required value="hello" disabled />
          </li>
          <li>
            <label for="tel">์ „ํ™”๋ฒˆํ˜ธ</label>
            <input type="tel" id="tel" required autofocus />
          </li>
          <li>
            <label for="url">url</label>
            <input
              type="url"
              id="url"
              required
              placeholder="url์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
            />
          </li>
          <li>
            <label for="number">number</label>
            <input type="number" id="number" required min="5" max="10" />
          </li>
          <li>
            <label for="range">range</label>
            <input type="range" id="range" required />
          </li>
          <li>
            <label for="password">password</label>
            <input type="password" id="password" required />
          </li>
        </ul>
      </fieldset>
      <fieldset>
        <legend>๋‚ ์งœ๊ด€๋ จ</legend>
        <ul>
          <li>
            <label for="date">date</label>
            <input type="date" id="date" min="2022-08-20" max="2022-08-30" />
          </li>
          <li>
            <label for="month">month</label>
            <input type="month" id="month" />
          </li>
          <li>
            <label for="week">week</label>
            <input type="week" id="week" />
          </li>
        </ul>
      </fieldset>
      <fieldset>
        <legend>์‹œ๊ฐ„๊ด€๋ จ</legend>
        <ul>
          <li>
            <label for="time">time</label>
            <input type="time" id="time" min="16:00" max="10:00" />
          </li>
        </ul>
      </fieldset>
      <fieldset>
        <legend>๊ทธ ์™ธ</legend>
        <ul>
          <li>
            <input type="file" multiple />
          </li>
          <li>
            <input type="hidden" name="" value="" />
          </li>
        </ul>
      </fieldset>
      <input type="submit" value="์ œ์ถœํ•˜๊ธฐ" />
    </form>
  </body>
</html>

Form ๋ฐ์ดํ„ฐ ํƒœ๊ทธ ์†์„ฑ

  • required
    • ์ž…๋ ฅ๊ฐ’์ด ํ•„์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹œ
  • readonly
    • ํ•„๋“œ๋ฅผ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ํ•„๋“œ๋กœ ๋งŒ๋“ฆ
  • disabled
    • ๋น„ํ™œ์„คํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•ด๋‹น ํ•„๋“œ๋Š” ์„œ๋ฒ„๋กœ ์ „์†ก๋˜์ง€ ์•Š์Œ
  • autofocus
    • ์ดˆ๊ธฐ์— ํ•ด๋‹น ํ•„๋“œ์— ์ปค์„œ๋ฅผ ์œ„์น˜ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ
  • placeholder
    • ์ž…๋ ฅ ํ•„๋“œ๊ฐ€ ๋น„์–ด์žˆ์„ ๋•Œ ํ•ด๋‹น ์ž…๋ ฅ๊ฐ’์˜ ์„ค๋ช… ๋˜๋Š” ๊ฐ€์ด๋“œ ๋ฌธ๊ตฌ๋ฅผ ์‚ฝ์ž… ํ•  ์ˆ˜ ์žˆ์Œ

checkbox

์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ฒดํฌ๋ฐ•์Šค ํ•ญ๋ชฉ ์ค‘ 2๊ฐœ ์ด์ƒ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Œ. ์„ ํƒ๋œ ์ฒดํฌ๋ฐ•์Šค์˜ value๊ฐ’์ด ์„œ๋ฒ„๋กœ ์ „์†ก

<ul>
	<li>
		<label for="apple">์‚ฌ๊ณผ</label>
		<input id="apple" type="checkbox" value="apple">
	</li>
	<li>
		<label for="orange">์˜ค๋ Œ์ง€</label>
		<input id="orange" type="checkbox" value="orange">
	</li>
	<li>
		<label for="banana">๋ฐ”๋‚˜๋‚˜</label>
		<input id="banana" type="checkbox" value="banana">
	</li>
</ul>

radio

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ผ๋””์˜ค ํ•ญ๋ชฉ ์ค‘ 1๊ฐœ๋ฅผ ์„ ํƒ ๊ฐ€๋Šฅ, ์„ ํƒ๋œ ์ฒดํฌ๋ฐ•์Šค์˜ value๊ฐ’์ด ์„œ๋ฒ„๋กœ ์ „์†ก

→ ์—ฌ๋Ÿฌ ๊ฐœ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๊ทธ ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์˜ <radio name=””> name ์†์„ฑ ๊ฐ’์„ ๊ฐ™์€ ๊ฐ’์œผ๋กœ ๊ทธ๋ฃนํ•‘ ํ•ด์ค˜์•ผ ํ•จ

<ul>
	<li>
		<label for="strawberry">๋”ธ๊ธฐ</label>
		<input id="strawberry" type="radio" name="fruit" value="strawberry">
	</li>
	<li>
		<label for="grape">ํฌ๋„</label>
		<input id="grape" type="radio" name="fruit" value="grape">
	</li>
	<li>
		<label for="peach">๋ณต์ˆญ์•„</label>
		<input id="peach" type="radio" name="fruit" value="peach">
	</li>
</ul>

Textarea

<textarea>๋Š” ์—ฌ๋Ÿฌ ์ค„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

<textarea id="story" name="story" rows="5" cols="33">
	It was a dark and stormy night...
</textarea>
  • rows: ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ํ–‰ ์ˆ˜
  • cols: ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ์ปฌ๋Ÿผ ์ˆ˜

Select

<select>ํƒœ๊ทธ๋Š” ์˜ต์…˜ ๋ฉ”๋‰ด๋ฅผ ์ œ๊ณต, <option>ํƒœ๊ทธ๋กœ ๊ฐ ํ•ญ๋ชฉ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, <select>ํƒœ๊ทธ๋Š” <option>ํƒœ๊ทธ๋ฅผ ๊ฐ์‹ธ์คŒ

<select name="goods" id="goods">
	<option value="10kg">์‚ฌ๊ณผ 10kg</option>
	<option value="20kg" selected>์‚ฌ๊ณผ 20kg</option>
	<option value="30kg">์‚ฌ๊ณผ 30kg</option>
	<option value="40kg">์‚ฌ๊ณผ 40kg</option>
	<option value="50kg">์‚ฌ๊ณผ 50kg</option>
</select>
  • selected ์†์„ฑ์€ ํ˜„์žฌ <select> ์š”์†Œ์—์„œ ์„ ํƒ๋œ ํ•ญ๋ชฉ(<option>)์„ ๊ฐ€๋ฆฌํ‚ด

datalist

๋‹ค๋ฅธ ์ปจํŠธ๋กค์—์„œ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅํ•œ, ํ˜น์€ ์ถ”์ฒœํ•˜๋Š” ์„ ํƒ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” <option>์š”์†Œ ์—ฌ๋Ÿฟ์„ ๋‹ด์Œ

<label for="ice-cream-choice">๋ง›์„ ์„ ํƒํ•˜์„ธ์š”</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
	<option value="chocolate">
	<option value="coconut">
	<option value="mint">
	<option value="strawberry">
	<option value="vanilla">
</datalist>

Button

<button>์š”์†Œ๋Š” ํด๋ฆญ ๊ฐ€๋Šฅํ•œ ๋ฒ„ํŠผ์„ ๋‚˜ํƒ€๋ƒ„, <form>๋‚ด๋ถ€์™€ ๋ฒ„ํŠผ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•œ ๊ณณ์ด๋ผ๋ฉด ์–ด๋””์—๋‚˜ ๋ฐฐ์น˜ ๊ฐ€๋Šฅ

<button type="button">
	์ถ”๊ฐ€ํ•˜๊ธฐ
</button>

Type

  • submit: ๋ฒ„ํŠผ์ด ์„œ๋ฒ„๋กœ ์–‘์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์ถœ(๊ธฐ๋ณธ ๊ฐ’)
  • reset: <input type=”reset”>์ฒ˜๋Ÿผ, ๋ชจ๋“  ์ž…๋ ฅ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋˜๋Œ๋ฆผ
  • button: ๊ธฐ๋ณธ ํ–‰๋™์ด ์—†์œผ๋ฉฐ ์ฃผ๋กœ ํด๋ฆญ ํ•œ ํ›„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ธก ์ฝ”๋“œ๋ฅผ ๋ช…๋ นํ•  ๋•Œ ์‚ฌ์šฉ
<!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>Radio & Checkbox</title>
  </head>
  <body>
    <form action="">
      <fieldset>
        <legend>์ข‹์•„ํ•˜๋Š” ์ƒ‰์ƒ์„ ๋ชจ๋‘ ์„ ํƒํ•ด์ฃผ์„ธ์š”.</legend>
        <ul>
          <li>
            <label for="red">๋นจ๊ฐ•</label>
            <input type="checkbox" id="red" value="red" />
          </li>
          <li>
            <label for="orange">์ฃผํ™ฉ</label>
            <input type="checkbox" id="orange" value="orange" />
          </li>
          <li>
            <label for="yellow">๋…ธ๋ž‘</label>
            <input type="checkbox" id="yellow" value="yellow" />
          </li>
          <li>
            <label for="blue">ํŒŒ๋ž‘</label>
            <input type="checkbox" id="blue" value="blue" />
          </li>
          <li>
            <label for="green">์ดˆ๋ก</label>
            <input type="checkbox" id="green" value="green" />
          </li>
        </ul>
      </fieldset>
      <fieldset>
        <legend>๋ฐฐ์†ก๋ฐฉ๋ฒ•์€?</legend>
        <ul>
          <li>
            <label for="free">๋ฌด๋ฃŒ</label>
            <input type="radio" name="delivery" id="free" value="free" />
          </li>
          <li>
            <label for="pay">์œ ๋ฃŒ</label>
            <input type="radio" name="delivery" id="pay" value="pay" />
          </li>
        </ul>
      </fieldset>
      <fieldset>
        <legend>์ž๊ธฐ์†Œ๊ฐœ์„œ</legend>
        <textarea name="" id="" cols="30" rows="5">์ €๋Š” ํ™๊ธธ๋™์ž…๋‹ˆ๋‹ค.</textarea>
      </fieldset>
      <fieldset>
        <legend>์ฃผ๋ฌธ ์ƒํ’ˆ์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”!</legend>
        <ul>
          <li>
            <select name="goods" id="goods" multiple>
              <option value="apple_10kg">์‚ฌ๊ณผ 10kg</option>
              <option value="apple_20kg" selected>์‚ฌ๊ณผ 20kg</option>
              <option value="apple_30kg">์‚ฌ๊ณผ 30kg</option>
              <option value="apple_40kg">์‚ฌ๊ณผ 40kg</option>
              <option value="apple_50kg">์‚ฌ๊ณผ 50kg</option>
            </select>
          </li>
          <li>
            <label for="ice-cream-choice">๋ง›์„ ์„ ํƒํ•˜์„ธ์š”</label>
            <input
              list="ice-cream-flavors"
              id="ice-cream-choice"
              name="ice-cream-choice"
            />
            <datalist id="ice-cream-flavors">
              <option value="chocolate"></option>
              <option value="coconut"></option>
              <option value="mint"></option>
              <option value="strawberry"></option>
              <option value="vanilla"></option>
            </datalist>
          </li>
        </ul>
      </fieldset>
      <button type="submit">์ œ์ถœํ•˜๊ธฐ</button>
      <button type="reset">๋ฆฌ์…‹ํ•˜๊ธฐ</button>
      <button type="button" onclick="alert('hello')">๋ฒ„ํŠผ</button>
    </form>
  </body>
</html>