Button element submitting form. How to deactivate?

Asked

Viewed 346 times

1

I need to separate the form into 2 different Ivs on the same page. I tried the next one and it didn’t work:

    <div id="div1">
<form method="post">
<input type="text" name="Nome">
</form>
<button onclick="exibediv2()">
</div>
    <div id="div2">
<form method="post">
<input type="text" name="Cidade">
<input type="submit">
</form>
<button onclick="exibediv1()">
</div>

When I use Submit in the second form, it does not recognize the value of the first input. How to solve this?

[Added due feedback from comments]

When I leave as one <form> the first button acts like a submit.

  • 1

    If I understand what you need you should avoid closing the first form and creating the second. I think you need something like this https://jsfiddle.net/6szwunqm/ without the </form>...<form> between the Ivs.

  • What function JS display1() and exibedv2() ?

  • Scripts only change the position of the Divs by setting the display to block or None.

  • I also thought about it, Sergio, but when I don’t close the form, the button that should display the div 2, works like a Submit.

1 answer

2

The element button is set by default to be of type Submit type="submit". Except for version earlier than IE8 where it is defined as button type="button".

To disable the default type, simply specify within the tag itself:

<button type="button" />

For the specific case of the question, a suggestion of how to solve:

<form method="post">
<div id="div1">
<input type="text" name="Nome">
<button type="button" onclick="exibediv2()">
</div>
<div id="div2">
<input type="text" name="Cidade">
<input type="submit">
<button type="button" onclick="exibediv1()">
</div>
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.