![](/img/trans.png)
[英]How do I get a variable from one function on a js file and use it for another function on another js file?
[英]How do I access the variable from one function to another
我有一個帶有說明的圖像管理器,可以在其中修改圖像和圖像文件的文本。
這個想法是為了能夠保存我在數據庫中所做的修改,但是我發現的方法是僅在修改圖像時保存。 如果不修改圖像,則無法保存文本的修改。
我想到能夠訪問click函數中的imagePopup變量並能夠解決問題,但是我不知道這是否可行。
也許您知道執行此操作的更好方法,但我沒有看到。
感謝您的幫助!
我給你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!"
});
}
}
})
})
})
嘗試在更改功能之外聲明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!"
});
}})
在每個函數的頂部聲明變量
var imagenPopup='';
$("#subirPopup").change(function(){
//.. add more code
});
$("#guardarPopup").click(function(){
//.. add more code
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.