简体   繁体   English

限制文件输入中的文件选择数量

[英]limit number file selections in a file input

Is there a way to limit the number of files you can select using the input file plus the multiple attribute. 有没有一种方法可以限制您可以使用输入文件加上multiple属性选择的文件数量。

<input type="file" accept="image/*" multiple/>

I am aware of one approach that is to check the length of files after selection using the onChange event. 我知道一种方法是使用onChange事件在选择后检查文件的长度。 While this works I would like to prevent user from selecting more than 5 image files in the file select dialog phase. 虽然这样做有效,但我想防止用户在文件选择对话框阶段选择5个以上的图像文件。

Is this possible? 这可能吗?

Unfortunately there is no way if limiting the input field as specified in W3C. 不幸的是,没有办法限制W3C中指定的输入字段。 At least not without Javascript. 至少不是没有Javascript。 The following would work but is probably not as nice as you would have liked: 以下方法可以工作,但可能不如您期望的那样好:

<input type="file" onchange="checkFiles(this.files)">

function checkFiles(files) {       
    if(files.length>5) {
        alert("length exceeded");
        files.slice(0,5);
        return false;
    }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM