javascript - 无法读取 null 的属性 'value'

标签 javascript jquery html ajax forms

我可能花了一个小时试图弄清楚这个问题......无论我尝试什么,我都会不断收到这个错误:

Cannot read property 'value' of null

这是我的 HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Bootstrap 101 Template</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
  <body>
    <h1>Hello, world!</h1>
    <div id="login_box"><form class="form-inline" action="JavaScript: login();" method="post">
  <input type="text" class="input-small" id="login_username" name="username" placeholder="Username">
  <input type="password" class="input-small" id="login_password" name="password" placeholder="Password">
  <label class="checkbox"><input type="checkbox" id="login_autologin" name="autologin"> Remember me  </label>
  <button type="submit" class="btn" name="login">Sign in</button>
</form></div>
    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/overall_js.js"></script>
  </body>
</html>

和相关的JS文件(overall_js.js):

function login() {
$("#login_box").html("logging you in....");
console.log(document.getElementById("login_username").value);
$.post("user.php?mode=login", {
    username : document.getElementById("login_username").value, 
    password : document.getElementById("login_password").value,
    autologin : document.getElementById("login_autologin").value,
}).done(function(data) {
    result = JSON.parse(data);
    if (result.status == 3) {
        $("#login_box").html(
                "Welcome back, " + result.user_row.username + "!");
    } else {
        $("#login_box")
                .html("You was not logged in: , " + result.error_msg);
    }
});
}

如果你能解决这个问题,谢谢!我也是 jQuery 和 JS 的新手,所以如果有人有任何建议使它更“标准”,我将不胜感激!

最佳答案

这一行

$("#login_box").html("logging you in....");

替换您的 <div> 的内容.您删除表单和所有输入元素...

关于javascript - 无法读取 null 的属性 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751398/

相关文章:

jquery - jquery 中的每个元素按顺序(从上到下)绑定(bind)元素?

javascript - 在 JavaScript 中添加和删除 body 中的 div

c# - 页面之间的套接字?

javascript - 如何加载新图像并将其绘制到 Canvas 上?

javascript - 尽管返回了一个对象,但期望在箭头函数中返回一个值

javascript - 动态插入字符串到 ACE 代码编辑器,angular JS

javascript - 如何从全局访问隐藏在对象内的变量?

jquery - Element.update 不是函数 [Rails 3 + JQuery]

javascript - 如何在 Bootstrap Material 设计的输入文本中制作标签支持?

html - 使用CSS旋转右边框