相当于jquery input.files [英] Jquery input.files equivalent
问题描述
在过去,我已经访问了由用户使用 input.files
,但是我不知道如何用JQuery来做这件事;
代码:
$(function(){
$(#cmdSubmit)。 (){
var file = document.getElementById(fileInput)。files [0];
alert(file); // A
file = $( (file); // b
file = $(#fileInput)。files [0];
alert(文件); // C
});
});
Option A 给我所期望的一个文件对象。然而,选项 B 只是简单地给我上传文件的名称,而且(据我所知)不是文件本身。
选项 C 显示 files
未定义。
什么是Jquery相当于 input.files
?
注意:我不反对使用native javascript;但是鉴于我在整个项目的其余部分都使用了JQuery,所以我希望在这里使用它,如果可能的话。
你必须访问元素。试试:
$ $ $ $ $ $ $ $ $ $ $#fileInput)[0] .files [0];
alert(file); // C
或(谢谢杰克)
file = $(#fileInput)。prop('files')[0];
alert(file);
I have a simple html form with a single file upload input. (jsfiddle)
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;
Code:
$(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 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.
Option C shows that files
is undefined.
What is the Jquery equivalent of input.files
?
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
or (thank you Jack)
file = $("#fileInput").prop('files')[0];
alert(file);
这篇关于相当于jquery input.files的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!