2011-10-06 79 views
4

在发送表单之前,有任何方法可以检查图像大小吗?如何在上传之前检查图像大小(例如1MB)

我使用jquery.form

JS:

$(document).ready(function() { 
    var options = { 
     target:  '#myform', 
    }; 
    $('#myformbutton').click(function() { 
     $('#myform').ajaxSubmit(options); 
     return false; 
    }); 
}); 

HTML:

<form action="myaction" method="post" id="myform" enctype="multipart/form-data"> 
    <label for="id_title">Title</label> 
    <input id="id_title" type="text" name="title" maxlength="255" /> 
    <label for="id_image">Image</label> 
    <input type="file" name="image" id="id_image" /> 
    <input type="button" id="myformbutton" value="Add!" /> 
</form> 

回答

12

我回答我的问题:

$(document).ready(function() { 
     $('#id_image').bind('change', function() { 
      if(this.files[0].size > 1000141){ 
       $('#formerror').html('File is too big'); 
       $('#myformbutton').hide(); 
      }else{ 
       $('#formerror').html(' '); 
       $('#myformbutton').show('slow'); 
      } 
     }); 
    }); 

和HTML:

 <div id="formerror"></div> 
     <form action="myaction" method="post" id="myform" enctype="multipart/form-data"> 
      <label for="id_title">Title</label> 
      <input id="id_title" type="text" name="title" maxlength="255" /> 
      <label for="id_image">Image</label> 
      <input type="file" name="image" id="id_image" /> 
      <input type="button" id="myformbutton" value="Add!" /> 
     </form> 

This Works。

+0

完美的作品!非常感谢! –

相关问题