【问题标题】:Check if a file input has a file attached检查文件输入是否附加了文件
【发布时间】:2018-06-27 19:33:48
【问题描述】:

需要检查file input 是否有附加文件。

此代码不起作用:

<input type="file" name="logo">

if ($('input[type=file]').length > 0) {
    if($(this).val() == '') {
        alert('Vide');
    }
    else {
        alert('Pas Vide');
    }
}

有什么帮助吗?

【问题讨论】:

    标签: jquery input


    【解决方案1】:

    FileList 对象有一个files 属性,您可以检查其长度。见docs on FileList

    这种类型的对象由 HTML &lt;input&gt; 元素的 files 属性返回

    这个 SO 答案很好地解决了这个问题Possible duplicate ?

    $("input:file").change(function (){
    if ($('input[type=file]').get(0).files.length > 0) {
        if($(this).val() == '') {
            alert('Vide');
        }
        else {
            alert('Pas Vide');
        }
    }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="file" name="logo">

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2014-11-17
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 2011-12-05
      • 2014-08-09
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多