简体   繁体   English

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

[英]How to check an input type=“file” has a file or not using jquery?

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

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

You should use "required" instead of JQuery. 您应该使用“required”而不是JQuery。 By just one attribute it'll check input=file has file or not. 只需一个属性就可以检查input = file是否有文件。 Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM