函数(evt)Javascript文件API
我对Javascript文件API有问题。首先,我检查表单输入是否有值,如果表单输入类型=文件名是image,我希望它得到图像:函数(evt)Javascript文件API,javascript,function,input,dom-events,fileapi,Javascript,Function,Input,Dom Events,Fileapi,我对Javascript文件API有问题。首先,我检查表单输入是否有值,如果表单输入类型=文件名是image,我希望它得到图像: function checkIfValued() { $("form input, textarea").change(function() { // ifs .... else if (name === "image") { checkQrCode(); } // els
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);
}
获取图像:
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函数中的事件有关(在最后一段代码中,事件直接绑定到函数,在第二段代码中,它有一个if语句)
无论如何,我如何修复它,如果有人能够解释event/evt选项,我们将不胜感激。您的
checkQrCode
函数希望接收触发的事件,该事件在事件回调中作为参数给出
当你这样称呼它时:
$("#qrCode").change(checkQrCode);
$("form input, textarea").change(function(evt) {
// ...
checkQrCode(evt);
。。。然后,回调函数将其传递给函数。但是,在第一个示例中,您忽略了事件并调用了事件预期函数,而没有参数
您希望它看起来更像这样:
$("#qrCode").change(checkQrCode);
$("form input, textarea").change(function(evt) {
// ...
checkQrCode(evt);
你能加把小提琴吗?在checkQRCode中,没有在任何地方定义事件,因此您可能正在从更高的函数范围访问变量。您确定工作代码不是更像
$(“#qrCode”)。更改(checkQRCode)
?有一个很大的区别,正如@KerryLiu所指出的,在这两个例子中,您都没有向函数传递事件。@numbers1311407您是对的,更改了它。那么如何在第一个代码中使其工作?当我把它放在第一个代码中时,它不起作用……在您的示例中,我假设您隐藏了name
变量来自//ifs
注释后面的位置,因为它不相关。不,您的小提琴不起作用。您正在测试name
,但没有定义它。(尽管奇怪的是,它在范围中被定义为“结果”)。如果您试图根据输入的名称运行二维码回调,请尝试$(this).attr(“name”)
或其他方法。在我的原始代码中执行了此操作,但忘记将其添加到此中。。。在实际添加一个名称
变量进行比较后,仍然无法准确执行代码所说的内容: