简体   繁体   English

读取文件上传-jQuery或Javascript

[英]Read file upload - jQuery or Javascript

I have a simple upload box: 我有一个简单的上传框:

<input type="file" id="signature" />

And use this jQuery: 并使用以下jQuery:

 $('#save').click(function() {
        var element = $('#signature');

        if (element.files && element.files[0]) {
            console.log("Can read image");
        }
        else {
            console.log("Failure");
        }
  }

It always hits "Failure". 它总是命中“失败”。 What am I doing wrong? 我究竟做错了什么?

element is jQuery's object, not DOM Element, so it does not have property files . element是jQuery的对象,而不是DOM元素,因此它没有属性files You can use: 您可以使用:

var element = $('#signature')[0];

Fiddle example . 小提琴的例子

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

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