Javascript File API に問題があります。まず、フォーム入力が評価されているかどうかを確認しています。フォーム入力タイプ=ファイル名が画像の場合、画像を取得したい:
function checkIfValued() {
$("form input, textarea").change(function() {
// ifs ....
else if (name === "image") {
checkQrCode();
}
// else ifs ....
});
}
画像の取得:
function checkQrCode(evt) {
var qrcode = evt.target.files[0]; // create a FileList and take the first one
if (!qrcode.type.match("image.*")) { // check to see if it is an image
var $parentDiv = $(this).parent().parent();
$parentDiv.removeClass("has-success");
$parentDiv.addClass("has-error");
return;
}
var reader = new FileReader(); // create a FileReader
reader.onload = function (evt) {
var img = new Image(); // create a new image
img.src = event.target.result; // set the src of the img to the src specified
if ($("#qrcode").siblings().length != 0) { // if qrcode has siblings (function already executed)
$("#qrcode").siblings().remove(); // remove the siblings
}
$("#qrcode").parent().append(img); // append the img to the parent
$("#qrcode").siblings("img").addClass("img-thumbnail");
$("#qrcode").siblings("img").css("float", "left");
}
reader.readAsDataURL(qrcode);
}
私が使用したとき:
$("#qrCode").change(checkQrCode);
うまくいきましたが、最初のコードを使用するとうまくいきません。checkQrCode関数のイベントと関係があると思います(最後のコードでは、イベントは関数に直接関連付けられており、2番目のコードでは途中でifステートメントがあります)。
とにかく、どうすれば修正できますか、誰かが event/evt オプションを説明できれば、非常にありがたいです。