gpt4 book ai didi

javascript - onclick提交按钮: Uncaught TypeError: Cannot read property 'value' of null

转载 作者:行者123 更新时间:2023-12-01 02:23:52 25 4
gpt4 key购买 nike

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

名称应该为空,但事实并非如此。有人有什么想法我可以解决这个问题吗?我是 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+"')");
}

非常感谢菲利克斯

最佳答案

您需要向元素添加 ID 才能使用函数 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>

关于javascript - onclick提交按钮: Uncaught TypeError: Cannot read property 'value' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com