2015-07-28 73 views
1

當我上傳大小超過1Mb的圖像時,警報正在工作,圖像也上傳了。誰能幫幫我嗎?返回false不起作用,但警報正在工作

$(document).on('change', '#images', function(){ 
    files = this.files; 
    size = files[0].size; 

    if (size < 1000141) { 
     return true; 
     // exit(); 
     // end; 
    } 

    alert('Please upload less than 1mb file'); 
    return false; 
    return true; 
}); 

//to show the preview of uploaded image 
function readURL(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 
     reader.onload = function (e) { 
     $('#blah').attr('src', e.target.result); 
     $('#blah').show(); 
    } 

    reader.readAsDataURL(input.files[0]); 
} 
} 

$("#images").change(function(){ 
    readURL(this); 
}); 
+3

您寫了返回false並在alert後返回true? –

+0

on是一個聽衆,所以你的onchange仍然會繼續。爲什麼不能在調用readURL之前進行驗證,而不是作爲單獨的監聽器。 – 2015-07-28 08:53:20

+1

'return true' after'return false' –

回答

-1

return false之後您得到了return true。將其更改爲如下

$(document).on('change', '#images', function(){ 
    files = this.files; 
    size = files[0].size; 

    if (size < 1000141) { 
     return true; 
     // exit(); 
     // end; 
    } 

    alert('Please upload less than 1mb file'); 
    return false; 


}); 
//rest of your code