Javascript 如何使用 JQuery 检测文件输入是否选择了文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4357245/
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
How do I use JQuery to detect if a file input has a file selected?
提问by TIMEX
And then alert the file name?
然后提示文件名?
回答by Blender
This should suffice:
这应该足够了:
alert($('#your_input_box').val());
A file selection box is just an input box, from what I can tell. Read more from this question: jQuery: get the file name selected from <input type="file" />.
据我所知,文件选择框只是一个输入框。从这个问题中阅读更多内容:jQuery: get the file name selected from <input type="file" />。
回答by Blender
To detect if a file was selected, you can find out the length of the file input
要检测是否选择了文件,您可以找出文件输入的长度
$("#bCheck").click(function() { // bCheck is a input type button
var fileName = $("#file1").val();
if(fileName) { // returns true if the string is not empty
alert(fileName + " was selected");
} else { // no file was selected
alert("no file selected");
}
});