onclick提交按钮:未捕获TypeError:无法读取属性'值' null

时间:2018-02-25 13:39:47

标签: javascript html html5

Uncaught TypeError: Cannot read property 'value' of null
    at register (VM43 js.js:66)
    at HTMLInputElement.onclick ((index):18)

名称应为null但不是。有人想我怎么解决这个问题?我是html / js的新手

<form>
  <input name="Name" type ="text" value="Name">
  <input name="Description" type="text" value="Description">
    <select name="Art" size="3">
      <option value="photo">Photo</option>
      <option value="video">Video</option>
    <input type="datetime-local" name="DateTime">
    <input type="submit" value="Upload" name="Upload" onclick="register();">
</form>

JS:

function register(){
  var name = document.getElementById('Name').value;//document.getElementsByTagName("firstname")[0].firstChild.data;
  var desc = document.getElementById('Description').value;
  var date = document.getElementById('DateTime').value;
  request.open("post", "db.php", false);
  request.onreadystatechange = scoreBoard;
  request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  //request.send("INSERT INTO Accounts VALUES('3', 'fl', 'pass')");
  console.log("f:"+f+" l:"+l+" e:"+e+" p:"+p);
  request.send("INSERT INTO `Uplouds`(name, descr, date) VALUES('"+name+"', '"+desc+"', '"+date+"')");
}

非常感谢 菲利克斯

1 个答案:

答案 0 :(得分:2)

您需要在元素中添加IDs才能使用getElementById函数:

这就是您获得null的原因,因为没有包含这些ID的元素:NameDescriptionDatetime

<form>
  <input id="Name" name="Name" type ="text" value="Name">
  <input id="Description" name="Description" type="text" value="Description">
    <select id="Art" name="Art" size="3">
      <option value="photo">Photo</option>
      <option value="video">Video</option>
    <input type="datetime-local" name="DateTime" id="DateTime">
    <input type="submit" value="Upload" name="Upload" onclick="register();">
</form>