简体   繁体   English

如何将函数内部变量的值获取到 on jquery 外部

[英]how can i get the value of variable inside function to outside the on jquery

can i take the value from 'a = entry.username' out of the function?我可以从函数中取出'a = entry.username'的值吗? i want to get value that already looping inside the function我想获得已经在函数内部循环的值

$('#btnlogin').click(function(){
        var usernamelogin = $('#usernamelogin').val()
        var passwordlogin = $('#passwordlogin').val()
        var data_user1 = JSON.parse(localStorage.getItem('data_user'))
        
        for(var i in data_user1){
        var entry = data_user1[i]
        
        if(usernamelogin == entry.username && passwordlogin == entry.password){
            window.location.href = "curhat.jsp"
            a = entry.username
            return
            }   
        }
        if(usernamelogin != entry.username && passwordlogin != entry.password){
            alert("wrong username or password") 
        }
                
        //console.log(data_user1)
        //console.log('username = ' + entry.username)
        
    })

Just declare a before you enter the function so that you can use it later as shown below:只需在输入函数之前声明a ,以便以后可以使用它,如下所示:

    let a;
    $('#btnlogin').click(function(){
            var usernamelogin = $('#usernamelogin').val()
            var passwordlogin = $('#passwordlogin').val()
            var data_user1 = JSON.parse(localStorage.getItem('data_user'))
            
            for(var i in data_user1){
            var entry = data_user1[i]
            
            if(usernamelogin == entry.username && passwordlogin == entry.password){
                window.location.href = "curhat.jsp"
                a = entry.username
                return
                }   
            }
            if(usernamelogin != entry.username && passwordlogin != entry.password){
                alert("wrong username or password") 
            }
                    
            //console.log(data_user1)
            //console.log('username = ' + entry.username)
            
        })

//check if a is not null and do something with it
if(a){
  //do something with a here out of the function
}

if you wish to pass a across pages, you may use localStorage or sessionStorage as shown below to store the value of a:如果您希望跨页面传递 a,您可以使用 localStorage 或 sessionStorage 如下所示来存储 a 的值:

a = entry.username;

localStorage.setItem("a",JSON.stringify(a))

and then later on retrieve it in another page using然后稍后使用在另一个页面中检索它

const val = JSON.parse(localStorage.getItem("a"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jquery:如何在函数内部存储变量,并在外部使用它? - Jquery: how can I store a variable inside a function, and use it outside? 如何在函数IN jquery之外获取变量值 - How to get variable value outside a function IN jquery 如何在函数外部获取变量的值? - How can I get the value of the variable outside of the function? 如何在函数内获取此变量的值? - How can I get the value from this variable inside function? 无法在jquery函数外部获取变量的值 - Unable to get the value of variable outside jquery function 如何在 JS 中运行 GET 请求并将值存储在 GET 请求之外的 JS 变量中 function - How can I run a GET Request in JS and store the value in a JS variable outside of the GET Request function 如何在 function 中使用从 function 之外的 select 下拉列表中选择的变量值并将其分配给 function 变量? - How can i use variable value inside function selected from select dropdown outside the function and assign it to a javascript variable? "如何获取mern堆栈中箭头函数之外的变量值" - how can i get the value of a variable outside the arrow function in mern stack 如何从 function 内部获取外部变量 - How to get variable outside from inside the function 如何获取Javascript中EventListener function之外的变量值? - How do I get the variable value outside of EventListener function in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM