我已经创建了一个空数组,也是一个全球性阵列。然后使用HTML表单中的三个空对象填充空数组。在谷歌浏览器中,它打印出HTML表单中输入的三个对象,但HTML不会将其打印在表格中。JavaScript对象不打印
如何使用的getElementById HMTL下面 出生登记的简单的日期
<body>
<h1>Birth Registration</h1>
<hr />
<form id ="inputFrom">
<label>First Name:
<input type='text' id='firstname' value='Enter your name Here' />
</label>
<label>Last Name:
<input type='text' id='lastname' value='Enter your name Here' />
</label>
<label for="size_1">D.O.B:</label><input type="date" name="size" id="birthDate" value="dd/mm/yy" />
<input type='button' onclick='regBirth()' value='Add new person'/>
</form>
<hr />
<table id="details">
<tr>
<th>First NameName</th>
<th>Last Name</th>
<th>Date of Birth</th>
</tr>
</table>
<h4>Statistics</h1>
<hr />
<h5>Total Count:</h5><p id="count"></p>
</body>
</html>
JS下面
var allPeople = [];
function regBirth(){
var myArray = [];
myArray.fname = document.getElementById('firstname').value ;
myArray.lname =document.getElementById('lastname').value;
myArray.dob=document.getElementById('birthDate').value;
console.log(myArray);
console.log(allPeople);
var inputForm = document.getElementById("inputFrom").reset();
tabularForm = document.createDocumentFragment();
//Table structure variables
var tablerow;
for (i=0;i<allPeople.length;i++){
var iteration = allPeople[i];
tablerow = document.createElement('tr');
//first table cell
myArray.fname = document.createElement('td');
myArray.fname .innerHTML = iteration;
tablerow.appendChild(myArray.fname);
//Second table cell
myArray.lname = document.createElement('td');
myArray.lname.innerHTML = iteration;
tablerow.appendChild(myArray.lname);
//Third table cell
myArray.dob = document.createElement('td');
myArray.dob.innerHTML = iteration;
tablerow.appendChild(myArray.dob);
//Table row close
tabularForm .appendChild(tablerow);
}
document.getElementById("details").appendChild(tabularForm);
}
谢谢76484..That是辉煌..是否有可能使用for循环输出数组长度? – IslandKing
没有必要使用for循环。 'length'是Array对象的一个属性。你可以像这样得到它:allPeople.length – 76484