reload - javascript: how to stop the page from reloading? - Stack Overflow

I just wrote a small program which takes the input from user and shows the factorial table. Here is the

I just wrote a small program which takes the input from user and shows the factorial table. Here is the code:

<h1>Table of factorials</h1>
<h3>Enter the number:<h3>
<form name="form1">
    <input type="text" name="num" onchange="display(parseInt(document.form1.num.value))"/>
    <input type="button" />
</form>
<script>
    function factorial(n){
        if(n<=1) return n;
        else return n*factorial(n-1);
    }
    function display(x){
        document.write("<table>");
        document.write("<tr><th>n</th><th>n!</th></tr>");
        for(var i =1;i<=x;i++){
            document.write("<tr><td>"+i+"</td><td>"+factorial(i)+"</td></tr>");
        }
        document.write("</table>");
        document.write("Generated at"+ new Date());
    }


</script>

But the problem is the form reloads again immediately after showing the table. But if i use the button to trigger the function by onclick="display(parseInt(document.form1.num.value))" it does fine.
How to fix this?

EDIT: Just noticed that it works fine in firefox too. The problem is with chrome, opera and safari

I just wrote a small program which takes the input from user and shows the factorial table. Here is the code:

<h1>Table of factorials</h1>
<h3>Enter the number:<h3>
<form name="form1">
    <input type="text" name="num" onchange="display(parseInt(document.form1.num.value))"/>
    <input type="button" />
</form>
<script>
    function factorial(n){
        if(n<=1) return n;
        else return n*factorial(n-1);
    }
    function display(x){
        document.write("<table>");
        document.write("<tr><th>n</th><th>n!</th></tr>");
        for(var i =1;i<=x;i++){
            document.write("<tr><td>"+i+"</td><td>"+factorial(i)+"</td></tr>");
        }
        document.write("</table>");
        document.write("Generated at"+ new Date());
    }


</script>

But the problem is the form reloads again immediately after showing the table. But if i use the button to trigger the function by onclick="display(parseInt(document.form1.num.value))" it does fine.
How to fix this?

EDIT: Just noticed that it works fine in firefox too. The problem is with chrome, opera and safari

Share Improve this question edited Jan 20, 2012 at 6:01 tarashish asked Jan 20, 2012 at 5:53 tarashishtarashish 1,96521 silver badges32 bronze badges 5
  • jsfiddle/d2DqP/7 I tried in chrome, it just work fine. – xdazz Commented Jan 20, 2012 at 6:08
  • for me it shows the table and then goes back to the form again in your jsfiddle too :(..i want it to stop on the table – tarashish Commented Jan 20, 2012 at 6:10
  • Is the reload caused by a form submit event? – Jørgen Commented Jan 20, 2012 at 6:14
  • but there is no submit event in the form – tarashish Commented Jan 20, 2012 at 6:21
  • 1 @unussunu there is a input type button which on some browsers has the default action to submit. – khael Commented Jan 20, 2012 at 7:02
Add a ment  | 

3 Answers 3

Reset to default 4

Use <form onsubmit="return false;"> ... </form>

prevent the default action of the input button. You can do this by JQuery's preventDefault() function .Or just <form onsubmit="return false;"> ... </form>

It's the problem of document.write, have a look at what it means

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742345218a4426409.html

相关推荐

  • reload - javascript: how to stop the page from reloading? - Stack Overflow

    I just wrote a small program which takes the input from user and shows the factorial table. Here is the

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信