|
1 | | -# Form submission: event and method submit |
| 1 | +# Отправка формы: событие и метод submit |
2 | 2 |
|
3 | | -The `submit` event triggers when the form is submitted, it is usually used to validate the form before sending it to the server or to abort the submission and process it in JavaScript. |
| 3 | +При отправке формы срабатывает событие `submit`, это обычно используется для проверки (валидации) формы перед её отправкой на сервер или для предотвращения отправки и обработки её с помощью JavaScript. |
4 | 4 |
|
5 | | -The method `form.submit()` allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server. |
| 5 | +Метод `form.submit()` позволяет инициировать отправку формы из JavaScript. Мы можем использовать его для динамического создания и отправки наших собственных форм на сервер. |
6 | 6 |
|
7 | | -Let's see more details of them. |
| 7 | +Давайте посмотрим на них подробнее. |
8 | 8 |
|
9 | | -## Event: submit |
| 9 | +## Событие: submit |
10 | 10 |
|
11 | | -There are two main ways to submit a form: |
| 11 | +Есть два основных способа отправить форму: |
12 | 12 |
|
13 | | -1. The first -- to click `<input type="submit">` or `<input type="image">`. |
14 | | -2. The second -- press `key:Enter` on an input field. |
| 13 | +1. Первый -- нажать кнопку `<input type="submit">` или `<input type="image">`. |
| 14 | +2. Второй -- нажать `key:Enter`, находясь на каком-нибудь поле. |
15 | 15 |
|
16 | | -Both actions lead to `submit` event on the form. The handler can check the data, and if there are errors, show them and call `event.preventDefault()`, then the form won't be sent to the server. |
| 16 | +Оба действия сгенерируют событие `submit` на форме. Обработчик может проверить данные, и если есть ошибки, показать их и вызвать `event.preventDefault()`, тогда форма не будет отправлена на сервер. |
17 | 17 |
|
18 | | -In the form below: |
19 | | -1. Go into the text field and press `key:Enter`. |
20 | | -2. Click `<input type="submit">`. |
| 18 | +В примере ниже: |
21 | 19 |
|
22 | | -Both actions show `alert` and the form is not sent anywhere due to `return false`: |
| 20 | +1. Перейдите в текстовое поле и нажмите `key:Enter`. |
| 21 | +2. Нажмите `<input type="submit">`. |
| 22 | + |
| 23 | +Оба действия показывают `alert` и форма не отправится благодаря `return false`: |
23 | 24 |
|
24 | 25 | ```html autorun height=60 no-beautify |
25 | 26 | <form onsubmit="alert('submit!');return false"> |
26 | | - First: Enter in the input field <input type="text" value="text"><br> |
27 | | - Second: Click "submit": <input type="submit" value="Submit"> |
| 27 | + Первый: Enter в текстовом поле: <input type="text" value="Текст"><br> |
| 28 | + Второй: Нажмите "Отправить": <input type="submit" value="Отправить"> |
28 | 29 | </form> |
29 | 30 | ``` |
30 | 31 |
|
31 | | -````smart header="Relation between `submit` and `click`" |
32 | | -When a form is sent using `key:Enter` on an input field, a `click` event triggers on the `<input type="submit">`. |
| 32 | +````smart header="Взаимосвязь между `submit` и `click`" |
| 33 | +При отправке формы по нажатию `key:Enter` в текстовом поле, генерируется событие `click` на кнопке `<input type="submit">`. |
| 34 | + |
| 35 | +Это довольно забавно, учитывая что никакого клика не было. |
33 | 36 |
|
34 | | -That's rather funny, because there was no click at all. |
| 37 | +Пример: |
35 | 38 |
|
36 | | -Here's the demo: |
37 | 39 | ```html autorun height=60 |
38 | | -<form onsubmit="return false"> |
39 | | - <input type="text" size="30" value="Focus here and press enter"> |
40 | | - <input type="submit" value="Submit" *!*onclick="alert('click')"*/!*> |
| 40 | +<form onsubmit="alert('submit!');return false"> |
| 41 | + <input type="text" size="30" value="При нажатии Enter будет click"> |
| 42 | + <input type="submit" value="Отправить" *!*onclick="alert('click!')"*/!*> |
41 | 43 | </form> |
42 | 44 | ``` |
43 | 45 |
|
44 | 46 | ```` |
45 | 47 |
|
46 | | -## Method: submit |
| 48 | +## Метод: submit |
47 | 49 |
|
48 | | -To submit a form to the server manually, we can call `form.submit()`. |
| 50 | +Чтобы отправить форму на сервер вручную, мы можем вызвать метод `form.submit()`. |
49 | 51 |
|
50 | | -Then the `submit` event is not generated. It is assumed that if the programmer calls `form.submit()`, then the script already did all related processing. |
| 52 | +При этом событие `submit` не генерируется. Предполагается, что если программист вызывает метод `form.submit()`, то он уже выполнил всю соответствующую обработку. |
51 | 53 |
|
52 | | -Sometimes that's used to manually create and send a form, like this: |
| 54 | +Иногда это используют для генерации формы и отправки её вручную, например так: |
53 | 55 |
|
54 | 56 | ```js run |
55 | 57 | let form = document.createElement('form'); |
56 | 58 | form.action = 'https://google.com/search'; |
57 | 59 | form.method = 'GET'; |
58 | 60 |
|
59 | | -form.innerHTML = '<input name="q" value="test">'; |
| 61 | +form.innerHTML = '<input name="q" value="javascript info">'; |
60 | 62 |
|
61 | | -// the form must be in the document to submit it |
| 63 | +// перед отправкой формы, её нужно вставить в document |
62 | 64 | document.body.append(form); |
63 | 65 |
|
64 | 66 | form.submit(); |
|
0 commit comments