2015-04-22 26 views
1

我試圖輸出線用的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> 
+0

在頁面加載後不要使用文件撰寫。 http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – epascarello

+0

@epascarello如何顯示沒有它的文本? – GDesigns

+0

innerHTML/appendChild/etc – epascarello

回答

0

返回FALSE的處理程序不會對頁面刷新提交:

<form id="myForm" action="formvalidation.html" onsubmit="checkAge();return false;"> 
+0

頁面變空並只顯示文本 - 我如何使用表格 – GDesigns

2

不能使用document.write()打印當前頁面上。

調用document.write()將自動調用document.open(),它將清除當前文檔,從而清除當前內容。

或者,你可以使用HTML <div id="result"></div>和改變是這樣的值,

document.getElementById("result").innerHTML = "Some Text"; 
+0

在頁面上顯示文本我能做些什麼呢?這是我不明白的 – GDesigns