检查输入文件是否为空 jquery

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

check if input file filed is empty jquery

jqueryformsvalidationfile-io

提问by hyperrjas

I have a form with a file input and I want to check if it is empty when the form is submitted.

我有一个带有文件输入的表单,我想在提交表单时检查它是否为空。

I have this code:

我有这个代码:

$('form#new_comment').submit(function(e) {
   var $this = $(this);
   var $input =  $this.find('input').val();
 if($($input == '')) {
   alert ("you must choose a image");
   return false; 
   e.preventDefault(); 
  }    
});  

But it always says you must choose a image, even when I have chosen an image.

但它总是说you must choose a image,即使我选择了一个图像。

Where is the problem?

问题出在哪儿?

回答by Sarfraz

Change:

改变:

if($($input == '')) {

To:

到:

if($input == '') {

Since it is simple variable that holds text, you don't need to use jQuery function over it again.

由于它是保存文本的简单变量,因此您无需再次使用 jQuery 函数。