简体   繁体   English

如何从一个函数访问变量到另一个函数

[英]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.

相关问题 如何从 js 文件上的一个 function 获取变量并将其用于另一个 js 文件上的另一个 function? - How do I get a variable from one function on a js file and use it for another function on another js file? 如何在JavaScript中将变量从一个函数访问到另一个函数? - How to access the variable from one function to another function in Javascript? function 如何从 JavaScript 中的另一个 function 访问变量值? - How one function access a variable value from another function in JavaScript? 如何将局部变量值从一个函数传递给另一个函数? - How do I pass local variable value from one function to another? 如何将变量从一个函数传递给另一个函数? - How can I pass a variable from one function to another? 如何将变量从一个脚本传递到另一个脚本 - how do I pass a variable from one script to another 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? 如何将变量从一个组件传递到另一个组件? - How do I pass a variable from one component to another? 如何从另一个范围访问此功能? - How do I access this function from within another scope? 如何从另一个函数读取变量? - How do I read a variable from another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM