如何使这个功能工作?

时间:2015-05-05 02:57:22

标签: javascript jquery

当我运行它时,我得到错误getInfo未定义。那么我该如何实现呢?

(我知道这不是一个安全的登录,我现在不担心这个)

 document.getElementById("submit").addEventListener("click", function getInfo(){
                    var user = document.getElementById('username');
                    var username = user.value;
                    var pass = document.getElementById('password');
                    var password = pass.value;
                    return [username, password];
            });

            var userinfo = getInfo();
            var username = userinfo[0];
            var password = userinfo[1];

            console.log(username, password);

1 个答案:

答案 0 :(得分:2)

这个怎么样:

function getInfo(){
    var user = document.getElementById('username');
    var username = user.value;
    var pass = document.getElementById('password');
    var password = pass.value;
    return [username, password];
 });

 document.getElementById("submit").addEventListener("click", function (){ getInfo();});
 //or
 //document.getElementById("submit").addEventListener("click", getInfo);

 var userinfo = getInfo();
 var username = userinfo[0];
 var password = userinfo[1];

 console.log(username, password);