如何在python中检查输入类型文件之一是否为空

时间:2021-04-01 12:58:33

标签: javascript python html jquery

<form method="post">
<input type = "file" name ="file1">
<input type = "file" name ="file1">
<input type = "file" name ="file1">    
</form>   

我想检查是否有任何输入字段为空。

2 个答案:

答案 0 :(得分:0)

Vanilla JS(您必须填写所有输入):

var inputs = document.getElementById("form").getElementsByTagName("input");
function check(){
  for(let i = 0; i < inputs.length; i++){
    if(inputs[i].value == ''){
      console.log("input something!");
      return false;
    }
  }
}
<form method="post" id="form" onsubmit="return check()">
<input type = "file" name ="file1">
<input type = "file" name ="file1">
<input type = "file" name ="file1">  
<button>Submit</button>
</form>

jQuery:

var inputs = $('input');
function check(){
  for(let i = 0; i < inputs.length; i++){
    if($(inputs[i]).val() == ''){
      console.log("input something");
      return false;
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post" id="form" onsubmit="return check()">
<input type = "file" name ="file1">
<input type = "file" name ="file1">
<input type = "file" name ="file1">  
<button>Submit</button>
</form>

答案 1 :(得分:-1)

您可以为此使用模糊功能。

        $('input[type="file"]').blur(function(){
            if(!$(this).val()){
                //Put the code if not a file
            } else{
                //put the code here if file is present
            }
        });
    
<form method="post">
<input type = "file" name ="file1">
<input type = "file" name ="file1">
<input type = "file" name ="file1">    
</form>