如何计算选定的文件 [英] How to count selected files

查看:69
本文介绍了如何计算选定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单非常简单,输入方式如下:

I have very simple form, with input like this:

<input id="my_id" multiple="true" type="file" name="image_name[]" />

现在我有两个问题:


  1. 如何使用jQuery或纯JavaScript计算选定的文件?

  2. 如何限制文件选择,比方说10,因为现在它是无限的?


推荐答案

如果输入类型文件值以文件存储在数组中,密钥为 name

in case of input type file the value is stored in array as files with key name.

$('input#my_id').change(function(){
    var files = $(this)[0].files;
    if(files.length > 10){
        alert("you can select max 10 files.");
    }else{
        alert("correct, you have selected less than 10 files");
    }
});

小提琴示例: http://jsfiddle.net/nze2B/3/

这篇关于如何计算选定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆