我有以下代码不起作用.我希望当输入改变时图像出现在swal(Sweet Alert)中,但我不知道什么不起作用.我在控制台上收到以下错误:
Failed to execute'readAsDataURL' on 'FileReader': parameter 1 is not type 'Blob'
INPUT
<input id="input" type="file" style="display:none;" onchange="muda()">
Run Code Online (Sandbox Code Playgroud)
脚本
<script>
function muda(){
var thefile = document.getElementById('input');
var reader = new FileReader();
reader.onloadend = function(){
var imagem = reader.result;
}
if(thefile){
reader.readAsDataURL(thefile);
}
swal({
title: "Esta é a imagem que pretende inserir?",
text: "<img src='"+imagem+"' style='width:150px;'>",
html:true,
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
UPDATE
有了adaneo响应,我设法读取文件名添加.files[0];但我现在不知道如何获取图像路径,我试图放置一个名为image的变量,你可以在代码中看到但它转undefined
Select2 函数在附加新的选择标记时不会打开,并且控制台出现此错误(未捕获的类型错误:$(...).select2 不是函数)。
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.full.min.js'></script>
$(document).on('click', '#addNewItem', function() {
var a = $(".gh").val();
a++;
$('.asd').append('<select class="form-control select2" name="ProposalItemName[]">' +
'<optgroup label="Items">' +
'<option>test</option>' +
'<option>test</option>' +
'</optgroup>' +
'</select>');
$('.select2').select2();
});
Run Code Online (Sandbox Code Playgroud)