在我的java腳本中,我可以創建一個新的表格行,然後當點擊img時,我的彈出窗口顯示,我可以選擇一個圖像。Java Script爲每個圖像創建獨特的類
問題,因爲每當創建新行時,每個<img class ".number"
是相同的,如果我從文件上傳選擇圖像預覽圖像填充所有其他img src。
我將如何能夠使我的劇本readURL()
& addImage()添加唯一的類<img class="">
使得readURL()
腳本功能,可以事先知情同意這個類呢?
<table id="images" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="text-center">Image</td>
</tr>
</thead>
<tbody>
<?php $image_row = 0; ?>
<?php $image_row++; ?>
</tbody>
</table>
<script>
var image_row = <?php echo $image_row; ?>;
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$("#example' + image_row + '").attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#file-input").change(function() {
readURL(this);
});
function addImage() {
html = '<tr id="image-row' + image_row + '">';
html += '<td>';
html += '<img src="" class="img-thumbnail number" id="example' + image_row + '" style="width: 100px; height:100px; cursor: pointer;">';
html += '</td>';
html += '</tr>';
$('#images tbody').append(html);
image_row++;
}
$(document).ready(function() {
$('body').popover({
selector: "#example' + image_row + '",
placement: 'right',
html: 'true',
content: '<span class="btn btn-primary btn-file"><i class="fa fa-pencil"></i> <input onchange="readURL(this)" id="file-input" type="file" name="userfile[]"/></span> <button type="button" id="button-clear" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>'
});
});
</script>
使用計數器image_row。 like class =「img-thumbnail number」+ image_row –
@SuchitKumar如果我在哪裏做,我將如何在$('。number')中使用它,attr('src',e.target.result );' – user4419336
$('。number')。attr('src',e.target.result)會將src設置爲結果值,其中任何類都是number.addImage()。 –