简体   繁体   中英

jquery - small issue when submitting form

I am using the following jquery snippet to display an image on my upload form while it is processing and everything works fine except the image is shown even when the file field is empty so I was wondering how I could change this function to only show the image if the field was not empty.

Thx.

<script>
 $(document).ready(function() {
 $("form").submit(function() {
  $("#loadingbox").show(); 
    return true; 
  }); 
});
 </script>
$("form").submit(function() {
  if($('#field').val() != ''){  
    $("#loadingbox").show(); 
      return true; 
  } else {
      //show error here
      //cancels form submit
      return false;
  }
}); 

Something like this should work (if I understood the question correctly):

$(document).ready(function() {
   $("form").submit(function() {
      if(!!$("#idOfYourFileInput").val()) {
         $("#loadingbox").show(); 
      }
      return true; 
   }); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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