File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -282,8 +282,12 @@ if (!myInput.checkValidity()) {
282
282
如果调用这个方法,并且参数不为空字符串,浏览器就会认为控件没有通过校验,就会立刻显示该方法设置的报错信息。
283
283
284
284
``` javascript
285
- // HTML 代码如下
286
- // <input type="file" id="fs">
285
+ /* HTML 代码如下
286
+ <form>
287
+ <p><input type="file" id="fs"></p>
288
+ <p><input type="submit"></p>
289
+ </form>
290
+ */
287
291
288
292
document .getElementById (' fs' ).onchange = checkFileSize;
289
293
@@ -292,14 +296,16 @@ function checkFileSize() {
292
296
var files = fs .files ;
293
297
if (files .length > 0 ) {
294
298
if (files[0 ].size > 75 * 1024 ) {
295
- fs .setCustomValidity (' 文件不能大于75KB ' );
299
+ fs .setCustomValidity (' 文件不能大于 75KB ' );
296
300
return ;
297
301
}
298
302
}
299
303
fs .setCustomValidity (' ' );
300
304
}
301
305
```
302
306
307
+ 上面代码一旦发现文件大于 75KB,就会设置校验失败,同时给出自定义的报错信息。然后,点击提交按钮时,就会显示报错信息。这种校验失败是不会自动消除的,所以如果所有文件都符合条件,要将报错信息设为空字符串,手动消除校验失败的状态。
308
+
303
309
### validity 属性
304
310
305
311
控件元素的属性` validity ` 属性返回一个` ValidityState ` 对象,包含当前校验状态的信息。
You can’t perform that action at this time.
0 commit comments