最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Better way to check if some inputs type file is empty in jquery? - Stack Overflow

programmeradmin1浏览0评论

I know that is simple question but I need an advice from experienced people.

I have 3 input type file like:

<input type="file" name="1-files" />
<input type="file" name="2-files" />
<input type="file" name="3-files" />

I select all inputs (on my page I have also other inputs type file) which name ends with "-files" ( I wrote in Google Chrome console):

$("input[type='file'][name*='-files']").length

Ok. I select a file using 1-files input. After that, I run the following code in Google Chrome console:

$("input[type='file'][name*='-files']:empty").length

I expect to be 2 but appears 3.

Can you tell me why ?

I want to get all elements of input type file which values are empty. I used in short way the selector :empty but it seems not working properly.

Of course, I could use

var empty_count = 0;
$.each($("input[type='file'][name*='-files']"), function(k, v){
  if($(this).val() === '')
     empty_count++;
});

But I want the shortest way to do this without $.each.

Thank you

I know that is simple question but I need an advice from experienced people.

I have 3 input type file like:

<input type="file" name="1-files" />
<input type="file" name="2-files" />
<input type="file" name="3-files" />

I select all inputs (on my page I have also other inputs type file) which name ends with "-files" ( I wrote in Google Chrome console):

$("input[type='file'][name*='-files']").length

Ok. I select a file using 1-files input. After that, I run the following code in Google Chrome console:

$("input[type='file'][name*='-files']:empty").length

I expect to be 2 but appears 3.

Can you tell me why ?

I want to get all elements of input type file which values are empty. I used in short way the selector :empty but it seems not working properly.

Of course, I could use

var empty_count = 0;
$.each($("input[type='file'][name*='-files']"), function(k, v){
  if($(this).val() === '')
     empty_count++;
});

But I want the shortest way to do this without $.each.

Thank you

Share Improve this question asked Jun 11, 2013 at 12:33 Snake EyesSnake Eyes 16.8k39 gold badges118 silver badges231 bronze badges 2
  • 1 Input elements can't contain content, so they're always empty. The :empty doco says so explicitly... – nnnnnn Commented Jun 11, 2013 at 12:38
  • You should manage the emptiness with '.val' attribute – Jaay Commented Jun 11, 2013 at 12:39
Add a comment  | 

3 Answers 3

Reset to default 10

It matches three because :empty matches an element with no descendants, and am input element by definition cannot have descendants (so all are empty, and therefore match the :empty selector).

To find those elements without selected files, I'd suggest:

$("input[type='file'][name*='-files']").filter(function (){
    return !this.value
}).length;

References:

  • :empty selector.
  • filter().

you can use filter insted of $.each..

$("input[type='file'][name*='-files']").filter(function(k){
  return $(this).val() === '';
}).length;

consider the following codes, i always used this :

     $("input[type='file'][name*='-files']").each(function(){
        if($(this).val().length == 0){
           alert("field is empty");
        }

    });
发布评论

评论列表(0)

  1. 暂无评论