Перейти к основному содержимому
← 📄 HTML & the platform·Модуль A3 · Урок 8
ЗаданиеBuild a 'features I want' checkbox group: three checkboxes with same name='features' and values 'dark_mode', 'notifications', 'autosave'. Pre-check dark_mode. Each has a label.

<тип ввода=флажок>: одиночный и сгруппированный

75 XP7 min
Теория

Two checkbox patterns

Single — boolean opt-in:

<input type="checkbox" id="tos" name="accepted_tos" value="yes" required />
<label for="tos">I agree to the terms</label>

If checked: submits accepted_tos=yes. If unchecked: nothing is submitted at all (the key just doesn't appear in the body). required forces the user to check.

Group — multi-select:

<input type="checkbox" id="py" name="languages" value="python" />
<label for="py">Python</label>

<input type="checkbox" id="js" name="languages" value="javascript" />
<label for="js">JavaScript</label>

<input type="checkbox" id="go" name="languages" value="go" />
<label for="go">Go</label>

Same name, different value. Submitted as: languages=python&languages=javascript (if first two checked).

FastAPI receives a list

async def submit(languages: list[str] = Form([])):
    return {"langs": languages}     # ["python", "javascript"]

The empty list [] default is critical — if zero boxes checked, the key isn't in the body at all and FastAPI errors out without a default.

checked attribute

Pre-check on first render:

<input type="checkbox" name="newsletter" value="yes" checked />
🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.