Skip to content

Commit ed82d83

Browse files
committed
docs(bom): edit form
1 parent a2df417 commit ed82d83

1 file changed

Lines changed: 203 additions & 4 deletions

File tree

docs/bom/form.md

Lines changed: 203 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,71 @@ function submitForm(action) {
9696
}
9797
```
9898

99+
### willValidate 属性
100+
101+
控件元素的`willValidate`属性是一个布尔值,表示该控件是否会在提交时进行校验。
102+
103+
```javascript
104+
// HTML 代码如下
105+
// <form novalidate>
106+
// <input id="name" name="name" required />
107+
// </form>
108+
109+
var input = document.querySelector('#name');
110+
input.willValidate // true
111+
```
112+
113+
### validationMessage 属性
114+
115+
控件元素的`validationMessage`属性返回一个字符串,表示控件不满足校验条件时,浏览器显示的提示文本。以下两种情况,该属性返回空字符串。
116+
117+
- 该控件不会在提交时自动校验
118+
- 该控件满足校验条件
119+
120+
```javascript
121+
// HTML 代码如下
122+
// <form><input type="text" required></form>
123+
document.querySelector('form input').validationMessage
124+
// "请填写此字段。"
125+
```
126+
127+
下面是另一个例子。
128+
129+
```javascript
130+
var myInput = document.getElementById('myinput');
131+
if (!myInput.checkValidity()) {
132+
document.getElementById('prompt').innerHTML = myInput.validationMessage;
133+
}
134+
```
135+
136+
### setCustomValidity()
137+
138+
控件元素的`setCustomValidity()`方法用来定制校验失败时的报错信息。它接受一个字符串作为参数,该字符串就是定制的报错信息。如果参数为空字符串,则上次设置的报错信息被清除。
139+
140+
如果调用这个方法,并且参数不为空字符串,浏览器就会认为控件没有通过校验,就会立刻显示该方法设置的报错信息。
141+
142+
```javascript
143+
// HTML 代码如下
144+
// <input type="file" id="fs">
145+
146+
document.getElementById('fs').onchange = checkFileSize;
147+
148+
function checkFileSize() {
149+
var fs = document.getElementById('fs');
150+
var files = fs.files;
151+
if (files.length > 0) {
152+
if (files[0].size > 75 * 1024) {
153+
fs.setCustomValidity('文件不能大于75KB');
154+
return;
155+
}
156+
}
157+
fs.setCustomValidity('');
158+
}
159+
```
160+
99161
### validity 属性
100162

101-
表单元素和控件元素都有一个`validity`属性,返回一个`ValidityState`对象,包含当前校验状态的信息。
163+
控件元素的属性`validity`属性返回一个`ValidityState`对象,包含当前校验状态的信息。
102164

103165
该对象有以下属性,全部为只读属性。
104166

@@ -114,9 +176,30 @@ function submitForm(action) {
114176
- `ValidityState.valid`:布尔值,表示用户是否满足所有校验条件。
115177
- `ValidityState.valueMissing`:布尔值,表示用户没有填入必填的值。
116178

117-
### 表单的 novalidate 属性
179+
下面是一个例子。
180+
181+
```javascript
182+
var input = document.getElementById('myinput');
183+
if (input.validity.valid) {
184+
console.log('通过校验');
185+
} else {
186+
console.log('校验失败');
187+
}
188+
```
189+
190+
下面是另外一个例子。
118191

119-
表单元素的`novalidate`属性,可以关闭浏览器的自动校验。
192+
```javascript
193+
var txt = '';
194+
if (document.getElementById('myInput').validity.rangeOverflow) {
195+
txt = '数值超过上限';
196+
}
197+
document.getElementById('prompt').innerHTML = txt;
198+
```
199+
200+
### 表单的 HTML 属性 novalidate
201+
202+
表单元素的 HTML 属性`novalidate`,可以关闭浏览器的自动校验。
120203

121204
```html
122205
<form novalidate>
@@ -131,6 +214,119 @@ function submitForm(action) {
131214
</form>
132215
```
133216

217+
## enctype 属性
218+
219+
表单能够用四种编码,向服务器发送数据。编码格式由表单的`enctype`属性决定。
220+
221+
假定表单有两个字段,分别是`foo``baz`,其中`foo`字段的值等于`bar``baz`字段的值s是一个分为两行的字符串。
222+
223+
```
224+
The first line.
225+
The second line.
226+
```
227+
228+
下面四种格式,都可以将这个表单发送到服务器。
229+
230+
**(1)GET 方法**
231+
232+
如果表单使用`GET`方法发送数据,`enctype`属性无效。
233+
234+
```html
235+
<form
236+
action="register.php"
237+
method="get"
238+
onsubmit="AJAXSubmit(this); return false;"
239+
>
240+
</form>
241+
```
242+
243+
数据将以 URL 的查询字符串发出。
244+
245+
```http
246+
?foo=bar&baz=The%20first%20line.%0AThe%20second%20line.
247+
```
248+
249+
**(2)application/x-www-form-urlencoded**
250+
251+
如果表单用`POST`方法发送数据,并省略`enctype`属性,那么数据以`application/x-www-form-urlencoded`格式发送(因为这是默认值)。
252+
253+
```html
254+
<form
255+
action="register.php"
256+
method="post"
257+
onsubmit="AJAXSubmit(this); return false;"
258+
>
259+
</form>
260+
```
261+
262+
发送的 HTTP 请求如下。
263+
264+
```http
265+
Content-Type: application/x-www-form-urlencoded
266+
267+
foo=bar&baz=The+first+line.%0D%0AThe+second+line.%0D%0A
268+
```
269+
270+
上面代码中,数据体里面的`%0D%0A`代表换行符(`\r\n`)。
271+
272+
**(3)text/plain**
273+
274+
如果表单使用`POST`方法发送数据,`enctype`属性为`text/plain`,那么数据将以纯文本格式发送。
275+
276+
```html
277+
<form
278+
action="register.php"
279+
method="post"
280+
enctype="text/plain"
281+
onsubmit="AJAXSubmit(this); return false;"
282+
>
283+
</form>
284+
```
285+
286+
发送的 HTTP 请求如下。
287+
288+
```http
289+
Content-Type: text/plain
290+
291+
foo=bar
292+
baz=The first line.
293+
The second line.
294+
```
295+
296+
**(4)multipart/form-data**
297+
298+
如果表单使用`POST`方法,`enctype`属性为`multipart/form-data`,那么数据将以混合的格式发送。
299+
300+
```html
301+
<form
302+
action="register.php"
303+
method="post"
304+
enctype="multipart/form-data"
305+
onsubmit="AJAXSubmit(this); return false;"
306+
>
307+
</form>
308+
```
309+
310+
发送的 HTTP 请求如下。
311+
312+
```http
313+
Content-Type: multipart/form-data; boundary=---------------------------314911788813839
314+
315+
-----------------------------314911788813839
316+
Content-Disposition: form-data; name="foo"
317+
318+
bar
319+
-----------------------------314911788813839
320+
Content-Disposition: form-data; name="baz"
321+
322+
The first line.
323+
The second line.
324+
325+
-----------------------------314911788813839--
326+
```
327+
328+
这种格式也是文件上传的格式。
329+
134330
## 文件上传
135331

136332
用户上传文件,也是通过表单。具体来说,就是通过文件输入框选择本地文件,提交表单的时候,浏览器就会把这个文件发送到服务器。
@@ -145,11 +341,14 @@ function submitForm(action) {
145341
<form method="post" enctype="multipart/form-data">
146342
<div>
147343
<label for="file">选择一个文件</label>
148-
<input type="file" id="file" name="myFile">
344+
<input type="file" id="file" name="myFile" multiple>
149345
</div>
150346
<div>
151347
<input type="submit" id="submit" name="submit_button" value="上传" />
152348
</div>
153349
</form>
154350
```
155351

352+
上面的 HTML 代码中,file 控件的`multiple`属性,指定可以一次选择多个文件;如果没有这个属性,则一次只能选择一个文件。
353+
354+

0 commit comments

Comments
 (0)