我已經創建了一個空數組,也是一個全球性陣列。然後使用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