 |
สอบถามเรื่องถ้าอยากเขียน CODE ตัวนี้ให้สั้นลง (jquery preview upload mutiple file) |
|
 |
|
|
 |
 |
|
Code (PHP)
$(".img").on("change", function()
{
var imgpreview = $(this).parent().find('div');
var files = !!this.files ? this.files : [];
if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
if (/^image/.test( files[0].type)){ // only image file
var reader = new FileReader(); // instance of the FileReader
reader.readAsDataURL(files[0]); // read the local file
reader.onloadend = function(){ // set image data as background of div
/* var a = $(this.find('div').css( "background-color", "red" );*/
$((imgpreview)).css("background-image", "url("+this.result+")");
}
}
});
ตอนนี้แก้ไขได้เรียบร้อยแล้วครับ
|
 |
 |
 |
 |
Date :
2014-08-17 13:42:43 |
By :
asustak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<input type=file class=fileupload id=?????
เพิ่มสีแดง
เปลี่ยนเป็น เรียก คลาสแทน
Code (JavaScript)
var tmpID=''
$(".fileupload").on("change", function(){
tmpID = $(this).attr('id')=='uploadfile'? '' : 1;
var files = !!this.files ? this.files : [];
if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
if (/^image/.test( files[0].type)){ // only image file
var reader = new FileReader(); // instance of the FileReader
reader.readAsDataURL(files[0]); // read the local file
reader.onloadend = function(){ // set image data as background of div
$("#imagePreview"+tmpID).css("background-image", "url("+this.result+")");
}
}
});
|
 |
 |
 |
 |
Date :
2014-08-17 13:43:51 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|