Jquery,获取输入类型=“文件”字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6061654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery, Getting the value of a input type="file" field
提问by twsJames
Can anybody let me know if there is anything wrong with the following code. I'm trying to get the value of a input type="file" form tag but the alert keeps telling me it's empty
任何人都可以让我知道以下代码是否有任何问题。我正在尝试获取 input type="file" 表单标签的值,但警报一直告诉我它是空的
$('#submitForm').bind("click",function(){
var imgVal = $('#imageUpload').val();
alert(imgVal);
return false;
});
<input name="imageUpload" id="imageUpload" type="file" value="" class="text"/>
<input name="submit" type="submit" id="submitForm" value="Submit Photo" />
Thanks in advance
提前致谢
James
詹姆士
采纳答案by Code Maverick
Works for me.
为我工作。
$('#submitForm').bind("click",function() {
var imgVal = $('#imageUpload').val();
alert(imgVal);
return false;
});
回答by BnW
This should work on any browser
这应该适用于任何浏览器
$('#imageUpload').attr('value')
回答by pinmonkeyiii
You could also do:
var imgVal = document.getElementById("imageUpload");
Then to get the value do something like this:
alert(imgVal.value);
你也可以这样做:
var imgVal = document.getElementById("imageUpload");
然后为了获得价值做这样的事情:
alert(imgVal.value);
One thing to remember is that many browsers will give you the file, but it will hide the actual path with something like this:
C:/fakepath/filename.extension
要记住的一件事是,许多浏览器会给你这个文件,但它会用这样的东西隐藏实际路径:
C:/fakepath/filename.extension
回答by Mohsin Shoukat
when any user select file then to detect in jquery is file element value change or not put this code in jQuery file
当任何用户选择文件然后在 jquery 中检测文件元素值更改或不将此代码放入 jQuery 文件时
$(document).ready(function()
{
//$("#upload").attr("disabled","true");
$("#upload").attr("disabled","disabled");
$("#file").change(function()
{
//alert("someting change");
//value = $(this).val();//get fake path of file
//alert(value);
$("#upload").removeAttr("disabled"); //enabling upload button
});
});
file type button is enable and upload button of form is disable when user will select file then it's value will change and enable upload button which means then we can submit our form.
当用户选择文件时,文件类型按钮被启用,表单的上传按钮被禁用,然后它的值会改变并启用上传按钮,这意味着我们可以提交我们的表单。
回答by Pinaki
You can use
您可以使用
$('#imageUpload').value