[英]How do I access the variable from one function to another
I have an image manager with descriptions where I can modify the texts of the image and the image file. 我有一个带有说明的图像管理器,可以在其中修改图像和图像文件的文本。
The idea is to be able to save the modifications that I make in my database, but the way that I have found is to save only if I modify the image. 这个想法是为了能够保存我在数据库中所做的修改,但是我发现的方法是仅在修改图像时保存。 If I do not modify the image, I can not save modifications of the texts.
如果不修改图像,则无法保存文本的修改。
It occurred to me to be able to access the imagePopup variable within the click function and be able to solve the problem, but I do not know if that is possible. 我想到能够访问click函数中的imagePopup变量并能够解决问题,但是我不知道这是否可行。
Maybe you know a better way to do this and I'm not seeing it. 也许您知道执行此操作的更好方法,但我没有看到。
I appreciate the help! 感谢您的帮助!
I give you the JQUERY code: 我给你JQUERY代码:
/*=============================================
UPLOAD IMAGE
=============================================*/
$("#subirPopup").change(function(){
var imagenPopup = this.files[0];
/*=============================================
VALIDATE JPG O PNG
=============================================*/
if(imagenPopup["type"] != "image/jpeg" && imagenPopup["type"] != "image/png"){
$("#subirLogo").val("");
swal({
title: "Error al subir la imagen",
text: "¡La imagen debe estar en formato JPG o PNG!",
type: "error",
confirmButtonText: "¡Cerrar!"
});
/*=============================================
VALIDATE SIZE IMAGE
=============================================*/
}else if(imagenPopup["size"] > 2000000){
$("#subirLogo").val("");
swal({
title: "Error al subir la imagen",
text: "¡La imagen no debe pesar más de 2MB!",
type: "error",
confirmButtonText: "¡Cerrar!"
});
/*=============================================
PREVISUALIZATION
=============================================*/
}else{
var datosImagen = new FileReader;
datosImagen.readAsDataURL(imagenPopup);
$(datosImagen).on("load", function(event){
var rutaImagen = event.target.result;
$(".previsualizarPopup").attr("src", rutaImagen);
})
}
/*=============================================
SAVE CHANGES
=============================================*/
$("#guardarPopup").click(function(){
var tituloPopup = $("#tituloPopup").val();
var textoBotonPopup = $("#textoBotonPopup").val();
var rutaBotonPopup = $("#rutaBotonPopup").val();
var datos = new FormData();
datos.append("tituloPopup", tituloPopup);
datos.append("textoBotonPopup", textoBotonPopup);
datos.append("rutaBotonPopup", rutaBotonPopup);
datos.append("imagenPopup", imagenPopup);
$.ajax({
url:"ajax/popup.ajax.php",
method: "POST",
data: datos,
cache: false,
contentType: false,
processData: false,
success: function(respuesta){
if(respuesta == "ok"){
console.log(respuesta);
swal({
title: "Cambios guardados",
text: "¡La plantilla ha sido actualizada correctamente!",
type: "success",
confirmButtonText: "¡Cerrar!"
});
}
}
})
})
})
Try declaring imagenpopup outside the change function... 尝试在更改功能之外声明imagenpopup ...
var imagenPopup='';
$("#subirPopup").change(function(){
imagenPopup = this.files[0];
/*=============================================
VALIDATE JPG O PNG
=============================================*/
if(imagenPopup["type"] != "image/jpeg" && imagenPopup["type"] != "image/png"){
$("#subirLogo").val("");
swal({
title: "Error al subir la imagen",
text: "¡La imagen debe estar en formato JPG o PNG!",
type: "error",
confirmButtonText: "¡Cerrar!"
});
/*=============================================
VALIDATE SIZE IMAGE
=============================================*/
}else if(imagenPopup["size"] > 2000000){
$("#subirLogo").val("");
swal({
title: "Error al subir la imagen",
text: "¡La imagen no debe pesar más de 2MB!",
type: "error",
confirmButtonText: "¡Cerrar!"
});
/*=============================================
PREVISUALIZATION
=============================================*/
}else{
var datosImagen = new FileReader;
datosImagen.readAsDataURL(imagenPopup);
$(datosImagen).on("load", function(event){
var rutaImagen = event.target.result;
$(".previsualizarPopup").attr("src", rutaImagen);
})
}
/*=============================================
SAVE CHANGES
=============================================*/
$("#guardarPopup").click(function(){
var tituloPopup = $("#tituloPopup").val();
var textoBotonPopup = $("#textoBotonPopup").val();
var rutaBotonPopup = $("#rutaBotonPopup").val();
if(imagenPopup == ''){
var datos = new FormData();
datos.append("tituloPopup", tituloPopup);
datos.append("textoBotonPopup", textoBotonPopup);
datos.append("rutaBotonPopup", rutaBotonPopup);
datos.append("imagenPopup", imagenPopup);
$.ajax({
url:"ajax/popup.ajax.php",
method: "POST",
data: datos,
cache: false,
contentType: false,
processData: false,
success: function(respuesta){
if(respuesta == "ok"){
console.log(respuesta);
swal({
title: "Cambios guardados",
text: "¡La plantilla ha sido actualizada correctamente!",
type: "success",
confirmButtonText: "¡Cerrar!"
});
}
}
})
}) } else {
swal({
title: "Error al subir la imagen",
text: "Some Error",
type: "error",
confirmButtonText: "¡Cerrar!"
});
}})
Declare the variable on top of every function 在每个函数的顶部声明变量
var imagenPopup='';
$("#subirPopup").change(function(){
//.. add more code
});
$("#guardarPopup").click(function(){
//.. add more code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.