我正在使用javascipt。我将在browe图像按钮上方显示最近的图像作为预览图像。
这里有4个问题,当我浏览那个问题的图像时,它会显示预览图像。当我点击添加更多按钮时,它会附加另一个问题。
当我浏览该队列的图片时,它会返回 null 的错误。
在这里,这是我的javascript
$(document).ready(function() {
var n = 5;
var i = 2;
var j = 10;
var k = 2;
$('.addmore').click(function(e){
e.preventDefault();
var div = $('#appendData');
var html = '';
html += '';
html += '<div class="formPartd">';
html += '<div class="formPartd_left">';
html += '<div class="upload-img">';
html += '<label><?php __d('statictext', 'Upload Image', false); ?>'+n+'</label>';
html += '<span>';
html += '<?php if($langlist[$_SESSION['Config']['language']]=='Spanish'){ ?>';
html += '<img id="thumbnil'+j+'" src="<?php echo $this->webroot?>images/no_status_image_sp.jpg" class="uploadimg" alt="">';
html += '<?php }else{ ?>';
html += '<img id="thumbnil'+j+'" src="<?php echo $this->webroot?>images/no-pollimg.jpg" class="uploadimg" alt="">';
html += '<?php } ?>';
html += '<?php if($langlist[$_SESSION["Config"]["language"]]=="Spanish"){ $browseClass = "browse-btn02";}else{ $browseClass = "browse-btn"; } ?>';
html += '<input type="file" class="<?php echo $browseClass;?> validate[required]" onchange="showMyImage1(this,\'thumbnail'+j+'\')" name="data[question1][question_image'+n+']" id="" />';
html += '</span>';
html += '<div class="clear10"></div>';
html += '</div>';
html += '<div class="fildtpart3 pos">';
html += '<label><?php __d('statictext', 'Answer', false); ?>'+n+'</label>';
html += '<span><input name="data[question_ans1][answer'+n+']" type="text" class="validate[required]" id="" value=""></span>';
html += '<div class="clear10"></div>';
html += '</div>';
html += '</div>';
div.append(html);
i++;j++;n++;
$(document).trigger('updated');
});
});
function showMyImage1(fileInput,id) {
var files = fileInput.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var imageType = /image.*/;
if (!file.type.match(imageType)) {
continue;
}
//alert(id);
var img = document.getElementById(id);
//alert(img);
img.file = file;
var reader = new FileReader();
reader.onload = (function(aImg) {
//alert(aImg);
return function(e) {
aImg.src = e.target.result;
};
})(img);
reader.readAsDataURL(file);
}
}
<div class="formPartd_left">
<div class="upload-img">
<label>Upload Image 3</label>
<span><img id="thumbnil3" src="<images/no_status_image_sp.jpg" class="uploadimg" alt="">
<input type="file" class="browse-btn02 validate[required]" onchange="showMyImage1(this,'thumbnil3')" name="data[question1][question_image3]" id="profile_image" />
</span>
<div class="clear10"></div>
</div>
<div class="fildtpart3 pos">
<label>Answer3</label>
<span><input name="data[question_ans1][answer3]" type="text" class="validate[required]" id="" value=""></span>
<div class="clear10"></div>
</div>
</div>
<div class="fildtpart2" style="float:right">
<a href="" type="button " class="csv_button addmore " style="float:right;background:#5a9325">Add More
</a>
</div>
当我点击追加浏览按钮时,会出现错误信息
img is null
img.file = file;
但它适用于给定的html(“不附加”)。
那么,我该如何解决这个错误?