Jquery input.files 等效 [英] Jquery input.files equivalent

查看:27
本文介绍了Jquery input.files 等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单个文件上传输入的简单 html 表单.(jsfiddle)

I have a simple html form with a single file upload input. (jsfiddle)

过去,我使用 input.files 访问了用户选择的文件,但是我不知道如何使用 JQuery 执行此操作;

In the past, I have accessed the file selected by the user using input.files, however I am at a loss as to how to do this with JQuery;

代码:

$(function () {
    $("#cmdSubmit").bind("click", function () {
        var file = document.getElementById("fileInput").files[0];
        alert(file); //A

        file = $("#fileInput").val();
        alert(file); //B

        file = $("#fileInput").files[0];
        alert(file); //C

    });        
});​

Option A 给我我所期望的,一个文件对象.但是,选项 B 只是给了我上传文件的名称,(据我所知)不是文件本身.

Option A give me what I expect, a file object. However, option B simply gives me the name of the uploaded file, and (as far as I can tell) not the file itself.

选项 C 显示 files 未定义.

Option C shows that files is undefined.

input.files 的 Jquery 等价物是什么?

What is the Jquery equivalent of input.files?

注意:我不反对使用原生 javascript;但考虑到我在这个项目的其余部分都使用 JQuery,如果可能的话,我更愿意在这里使用它.

Note: I have no objection to using native javascript; but given that I am using JQuery throughout the rest of this project I'd prefer to use it here as well if possible.

推荐答案

您必须访问该元素.试试:

You have to access the element. Try:

file = $("#fileInput")[0].files[0];
alert(file); //C

或(谢谢杰克)

file = $("#fileInput").prop('files')[0];
alert(file);

小提琴

这篇关于Jquery input.files 等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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