我有一小段JavaScript,用於對文件輸入字段進行一些驗證。它在Chrome,Safari,Opera,Firefox中工作正常,但它在Internet Explorer 9和更低版本中不起作用...我使用的是Jquery 1.8.3,顯然自1.4.2以來.change屬性應該可以與IE 。我也試過$(「的FileInput」)。住(‘變’......
無法看到這是怎麼回事,歡迎任何建議!
jQuery(document).ready(function($){
// Detect sh*tty IE
if ($.browser.msie && $.browser.version <= 9) {
// Bind to property change
$(".fileInput").bind('propertychange', function() {
fileChange(this);
});
} else {
$(".fileInput").change(function() {
fileChange(this);
});
}
function fileChange($item) {
// Get the filename
var $fileName = $($item).val();
var $inputId = $($item).attr('id');
var $fakeName = $($item).val().split('\\').pop();
var $fileSize = (($item.files[0].size)/1024)/1024;
var $ext = $($item).val().split('.').pop().toLowerCase();
var $acceptFiles = ['jpg', 'jpeg'];
if ($.inArray($ext, $acceptFiles) == -1) {
alert('For security, we can only accept jpeg images');
// Reset the value of $item item
$($item).val('');
return;
}
// Make sure the file size isn't bigger than 1mb
if ($fileSize >= 1.8) {
alert("The image you've chosen is too big. \n\nWe accept images up to 2mb in size");
// Reset the value of $item item
$($item).val('');
return;
}
// Check that the file
if ($fileName != '') {
$fileNotification = $('<li>', { id: 'file_'+$inputId, class: 'fileNotification', text: $fakeName});
// Append it to the list
$('#filesList').append($fileNotification);
}
// Hide the file input
$($item).css({display : 'none'});
// Show the next field
$($item).next().show();
};
});
這工作,對待感謝,優秀的東西。是的,也有服務器端驗證,它最初只在服務器上進行驗證,但是我們添加了這個功能,以便在人們點擊「發送」按鈕之前使體驗更加直觀。再次感謝 – ThePHPUnicorn 2013-02-23 15:26:27