使用Vanilla JS显示HTML表单数据

大家好,我正在开发一个简单的javascript程序,它在其中添加并显示html表单中的数据。除了显示数据外,没有遇到任何问题。我已经尝试了一切,但失败了,有人可以帮助我。是用于添加和显示的JS代码。

</style>
<script type="text/javascript">
var i=0;
function AddressRegister(Name,Type,Address,Email,Mobile,Location)
{
this.Name = Name;
this.Type = Type;
this.Address = Address;
this.Email = Email;
this.Mobile = Mobile;
this.Location = Location;
}
var flag = 0;
var tabledata,button;
var dataArray = new Array();
function Add()
{
var Name=document.getElementById("name").value;
var Types=document.getElementsByName("type");
for(i = 0; i < Types.length; i++) { 
    if(Types[i].checked) 
    var Type = Types[i].value; 
}
var Address=document.getElementById("address").value; 
var Email=document.getElementById("email").value;
var Mobile=document.getElementById("mobile").value;
var Location=document.getElementById("location").value;

var data = new AddressRegister(Name,Type,Address,Email,Mobile,Location);
dataArray.push(data);
displayAddress();
}
function displayAddress()
{
if(flag==0)
{
tabledata = "<table style='position: fixed; background-color:lightgrey; border: 1px solid black; 
border-collapse: collapse;  margin-top: 25px;' border = '1'><tr><th>Name</th><th>Type</th> 
<th>Address</th><th>Email</th><th>Mobile</th><th>Location</th></tr>";
}

for(i=0;i<dataArray.length;i++)
{
var tempname=dataArray[i].Name;
var temptype=dataArray[i].Type;
var tempaddress=dataArray[i].Address;
var tempemail=dataArray[i].Email;
var tempmobile=dataArray[i].Mobile;
var templocation=dataArray[i].Location;
}
//console.log(tabledata);
if(flag==0){   

document.getElementById("name").value = "";

document.getElementsByName("type").checked = false;

document.getElementById("address").value = "";

document.getElementById("email").value ="";

document.getElementById("mobile").value ="";

document.getElementById("location").value = "";
}
count=0;
}
</script>
</head>
评论
给不了告白
给不了告白

Try moving your entire <script> as the last element in the <body>.

Your code references elements, which are not yet rendered when your script is being parsed. Your document.getElementById("name") and other ones return null.

点赞
评论