xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I'm continuing to develop my website with Catalyst. I wrote a js snippet to modify html as below:

<form> <table> <tbody><tr><td><button onclick="purchase_new('new_form')">new</spa +n></button></td> </tbody></table> </form> <p id="new_form"> </p> <script language="javascript"> function purchase_new(i){ var p_new = document.getElementById(i); var new_table = document.createElement('table'); var new_th1 = document.createElement('th') var new_th2 = document.createElement('th'); var new_th3 = document.createElement('th'); var new_head1 = document.createTextNode('name'); var new_head2 = document.createTextNode('price'); var new_head3 = document.createTextNode('count'); var new_tr = document.createElement('tr'); new_th1.appendChild(new_head1); new_th2.appendChild(new_head2); new_th3.appendChild(new_head3); new_table.appendChild(new_th1); new_table.appendChild(new_th2); new_table.appendChild(new_th3); new_table.appendChild(new_tr); p_new.appendChild(new_table); } </script>
According to my predict, the page will add a new table when button clicked. But the fact is new table would flash and disappear quickly when I click button. I suspect form element result in it because it is all OK if I remove form element.

Any ideas? Thanks!

BTW, I noticed this article . And I'll apologize for my desert vocabulary if my post before made you or you uncomfortable. But I really really want to express my appreciation on the monks' help!





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: [OT] How avoid form submitting?
by almut (Canon) on Jun 24, 2010 at 09:59 UTC

    I think your onclick handler needs to return false to prevent the form from being submitted.  Try

    onclick="purchase_new('new_form'); return false"
      Many thanks!




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction