HTML5: How to count the length of the files from the multiple-input field

前端 未结 3 1906
天涯浪人
天涯浪人 2020-12-11 07:06

How can I use jquery to count the length of the files from the multiple-input field?

alert($(\'#myForm input[type=file]\').files.length);


        
相关标签:
3条回答
  • 2020-12-11 07:33

    Try getting the native DOM element using the .get method:

    $('#myForm input[type=file]').get(0).files.length
    

    Note however that if you have multiple DOM elements matching your selector this will return the first one and if you have none it will obviously throw an exception.

    0 讨论(0)
  • 2020-12-11 07:38

    None of those answers work with the new html 5 multiple tag. So if you have something like

    <input type="file" name="filesToUpload[]" id="filesToUpload" multiple="multiple" size="20" />
    

    note the multiple tag, JQ fails at getting every one of this files, only gets one of them, I've pretty much tried all of the possible selectors now but to no avail.

    However as mentioned before, standard Javascript works:

     alert(document.getElementById('filesToUpload').files.length);
    
    0 讨论(0)
  • 2020-12-11 07:41

    you can try alert($('#myForm input[type=file]').length); or alert($('#myForm input[type=file]').size());

    or alert($('#file').size()); or alert($('#file').length);

    0 讨论(0)
提交回复
热议问题