我在使用JavaScript時遇到了一些麻煩,它應該打印出您寫入表單的任何信息,並且它會立即消失。無法弄清楚我在這裏做錯了什麼,請幫忙!用Javascript打印輸出表單 - 文字馬上消失
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Javascript</title>
</head>
<body>
<h1>Personal Data</h1>
<p>Fill in your name in the form below:</p>
<form>
<fieldset>
First name <input id="fname" type="text"><br>
Last name <input id="lname" type="text"><br>
Age <input id="age" type="number" value="0"><br>
<button onclick="myFunction()">Click</button><br>
<p id="demo"></p>
<script>
function myFunction() {
var age = document.getElementById("age").value;
var lname = document.getElementById("lname").value;
var fname = document.getElementById("fname").value;
document.getElementById("demo").innerHTML = "<p>My name is " + fname + " " + lname + " and I am " + age + " years old!</p>";
}
</script>
</fieldset>
</form>
</body>
</html>
沒有指定類型的按鈕將提交表單作爲默認操作。 – Teemu