我有一個表格,用戶可以在表格填寫完成後打印表格。我似乎無法驗證用戶是否完成。一旦用戶完成填充表格,我希望打印整個表格。現在它只打印最後一個條目。如何用javascript填充表格並將其打印出來
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
function validateForm()
{
var x = document.getElementById('fname').value;
if(x === null || x === "")
{
document.getElementById('error1').innerHTML = "Invalid Entry";
}
else
//tableCreate();
addMore();
}
function addMore()
{
if(confirm("Would you like to add more names?") === true)
{
document.getElementById('theForm').reset();
//tableCreate();
//console.log("ADD MORE");
}
else
tableCreate();
}
function tableCreate()
{
var N = document.getElementById('fname').value;
var L = document.getElementById('lname').value;
var D = document.getElementById('dob').value;
var ar = [N, L, D];
console.log(ar);
document.write('<table>');
document.write('<tr>');
document.write('<th>First Name</th>');
document.write('<th>Last Name</th>');
document.write('<th>Date of Birth</th>');
document.write('</tr>');
document.write('<tr');
for(var i = 0; i < ar.length; i++)
{
document.write('<br><td>' + ar[i] + '</td>');
}
document.write('</tr>');
document.write('</table>');
}
</script>
</head>
<body>
<form name="theForm" action="FormAndTable.html" method="post">
First Name: <input type="text" id="fname">
<span style="color:red" id="error1"></span><br>
Last Name: <input type="text" id="lname"><br>
Date of Birth: <input type="text" id="dob"><br>
<input type="button" value="Save" onclick="validateForm()">
</form>
</body>
</html>
你'別人/ * tableCreate(); */addMore();'。當你註釋掉'tableCreate()'時,'addMore()'被移入'else'語句中。不知道這是否是需要的。 – Oriol
請參閱:[頁面加載後不允許document.write?](http://stackoverflow.com/questions/5985740/not-allowed-document-write-after-page-load) – Roberto
如果我的表單不是空的並提交我想問用戶他們是否想輸入另一個條目。這就是爲什麼之後,我調用函數addMore() – Avi