JS变量返回对象不是值

时间:2016-05-17 17:52:48

标签: javascript jquery variables global

我正在通过jQuery设置一个全局变量:

user = $("#user").val();

特别是没有var,因此可以将其拉出function

当我稍后尝试拉取该变量时,我得到的是[object HTMLInputElement]

这是我用来拉动变量的代码,

<script type="text/javascript">
      document.getElementById("user-login-info").innerHTML = "Hello, "+user;
</script>

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我检查过并修改了一下

$("#login-submit").click(function() {
                    user = $("#user").val();
                    pass = $("#pass").val();
                    var dataString = "user="+user+"&pass="+pass;
                    var linkString = "index.html#home";
                    if (user == "" || pass == "") {
                        alert("Please fill in all fields");
                    }
                    else {
                        $.ajax({
                            type: "POST",
                            url: "http://www.weddingindustryinsider.com/VAWEAPP/login.php",
                            data: {user: user, pass: pass},
                            cache: false,
                            success: function(result) {
                                if (result = "1") {
                                    alert("Login Sucess.");
                                    setTimeout(location.href = linkString,1000);
                                }
                                else { alert("Username or Password incorrect. Please try again."); }
                            },
                        });
                    }
                    return false;
                });