如何限制html中的文件上传次数?

Kin*_*arō 5 html5

我想限制用户在输入标签中选择最多6个文件.目前,我的输入标签是这样的:

<input type="file" name="question_pic" id="id_question_pic" multiple/>
Run Code Online (Sandbox Code Playgroud)

我想限制用户最多选择6个文件.我可以在服务器端返回错误,但我希望客户端先更改它.

有没有办法做到这一点?

谢谢.

Pra*_*man 5

您可以使用这样的jQuery函数:

$('.fileinput').change(function(){
    if(this.files.length>10)
        alert('Too many files')
});
// Prevent submission if limit is exceeded.
$('form').submit(function(){
    if(this.files.length>10)
        return false;
});
Run Code Online (Sandbox Code Playgroud)

  • 您可以通过清除文件列表值来阻止在更改函数中上传超过10个文件:`if(this.files.length> 10){alert('to many files'); this.value =''; 不幸的是,你不允许从filelist对象中删除项目,你只能重置整个值. (2认同)