开发者

Javascript... simple question how to keep the form on the page after submitting

Please see following example code that 开发者_如何学Pythontakes a year and a date and displays the data back when submitted...

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript</title>   
</head>
<body>
<FORM NAME="myform" ACTION="" METHOD="GET">
Enter the year to start from (eg: 2006): <BR>
<INPUT TYPE="text" NAME="year" VALUE=""><P>
Enter starting day (eg: 1= 1st Jan): <BR>
<INPUT TYPE="text" NAME="day" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Go!" onClick="testResults(this.form)">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {

  var years = form.year.value;
  var days = form.day.value;



   for (var x = days; x <= 365; x++)
   {

        for (var y = years; y <= 2010; y++)

        {

        document.write('  ValueY='+y+'ValueX='+x+'  ')

        }
    document.write("<br />");


}

}

function to3P(n){
return (n/100).toFixed(2).replace(/\./, '');
};
</SCRIPT>
</body>
</html>

What I'm trying to work out is how to LEAVE the form on the page after the document.write happens..(so the user doesn't have to keep pressing back to try new data!


It looks like you're using an example from 1995 to write your HTML... You should just read over some more modern html/javascript tutorials. Just a few good things to clean up:

  1. The language="javascript" attribute is depricated. You only need to specify <script type="text/javascript">
  2. It's better to use lowercase in your element declarations (ie: <p> instead of <P>)
  3. For every element that you open, you need to close. So for each <p>, you need a </p>. If there is no closing tag, ie, it is self-closing, as with the <br> tag, you should write it: <br />, just like you did in the document.write code. This makes your code truly XHTML compliant as you have specified in your doctype.
  4. As already stated, no reason to pass in the form to the javascript function. And just access the elements by id and their values via innerHTML.


document.write() is to blame. You should normally avoid it at all costs in favour of DOM manipulation and AJAX techniques. See here and here and particularly here and there's a metric ton of coverage on this subject around the web.


Don't use document.write - if you use it after the page loads, you're essentially creating a new document. You can either use the javascript DOM functions or just a

<div id="results"></div>


<script> document.getElementById('results').innerHTML = "Your text here"; </script>


Try to write in a div instead of the document, the form will still be visible

<input TYPE="button" NAME="button" Value="Go!" onClick="testResults(this.form)">
<div id="myResult/>

and in the javascript:

document.getElementById('myResult').innerHTML = 'your text here'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜