我試圖輸出線用的JavaScript文件撰寫的功能,但是,在提交表單,該行只是閃爍並消失的JavaScript - 1.4.3文件A線寫
有什麼想法?
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<script type="text/javascript">
function checkAge() {
var x = document.forms['myForm']['age'].value;
if(x < 18) {
document.write("<p>You are too Young. Form NOT submitted</p>");
} else {
document.write("Form sent successfully");
}
}
function tips() {
alert("Please enter your Name in the name Box, \nand Your age in the Age box.\nYou have to be Over 18 to submit the form");
}
</script>
<form id="myForm" action="formvalidation.html" onsubmit="checkAge();">
Name: <input type="text" name="text" id="name">
Age: <input type="text" name="age" id="age">
<input type="submit" value="Submit">
<input type="button" value="Help" id="hBtn" onclick="tips();">
</form>
</body>
</html>
在頁面加載後不要使用文件撰寫。 http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – epascarello
@epascarello如何顯示沒有它的文本? – GDesigns
innerHTML/appendChild/etc – epascarello