jQuery 如何使用jquery检查输入类型=“文件”是否有文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2642970/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:55:49  来源:igfitidea点击:

How to check an input type="file" has a file or not using jquery?

jqueryvalidationinput-type-file

提问by ACP

I have a file upload control <input id="File1" type="file" />in my page... How to check an input type="file" has a file or not using jquery on click of a button upload?

我的页面中有一个文件上传控件<input id="File1" type="file" />...如何在单击按钮时使用 jquery 检查输入类型 =“文件”是否有文件upload

回答by Quentin

if (jQuery('#File1').val()) { /* There are files */ }

回答by Hardik Sondagar

You should use "required" instead of JQuery. By just one attribute it'll check input=file has file or not. Example:

您应该使用“必需”而不是 JQuery。仅通过一个属性,它就会检查 input=file 是否有文件。例子:

<input type="file" required/>

回答by jAndy

$('#upload').bind('click', function(e){
   if( $('#File1').val() != ""){
       // file selected
   }
   else{
       // no file selected
   }
});