Cancel bootstrap JS default procedure

Asked

Viewed 215 times

0

Good morning,

I’m learning to program web now( studied C# last year in college and this year is web, so I wanted to get ahead ), I will program in Asp.net, I did all my "form" in bootstrap, visually stayed as I wanted, all right, I created the event click on the button to program it, but everything I do it sends the form and erases the data, any of the buttons that click, even without I program anything for them, talking to a colleague of mine he said this is on account of js , that by default sends the form, if there is more than one button all will send, and I need to replace this default ! just that I don’t know much about you, is that what he said? and how would I do? Follow the video showing what happens.

  • 1

    Edit your question and post your code. If you are using the element button and he’s inside the element form, add the attribute type="button"

  • You are probably using the same ID on all buttons!

1 answer

0


By default, the form buttons will perform the action of "Submit", ie, take the data that are in the form and perform a "post".

Here is an example of how to prevent this default behavior using Jquery.

$('#form-post #btn-post').click(function(e){
    e.preventDefault();
    alert('Previnido comportamento padrão!')

});
<form id="form-post" action="/pagina-processa-dados-do-form" method="post">
    <div>
        <label for="name">Nome:</label>
        <input type="text" id="name" />
    </div>
    <div>
        <label for="mail">E-mail:</label>
        <input type="email" id="mail" />
    </div>
    <div>
        <label for="msg">Mensagem:</label>
        <textarea id="msg"></textarea>
    </div>
    <div class="button">
        <button id="btn-post" type="submit">Enviar sua mensagem</button>
    </div>
</form>
<script  src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

  • https://codepen.io/capoia/pen/yvaEJR @Jorge, I did what you said, it really worked, it stopped giving "Submit" but I’m programming in Asp.net ( c# ) and even if I took the default wipe button, and programming it in the Asp part, ( ta in the css tab of codepen pq has no support) he does not clear the fields, because?

  • @Wellingtoncapoia, as it comes to actions carried out on the front, clear the fields using same Javascript. Here has an example I created using Jquery.

  • right but I was afraid that the other things q do in the back tmb does not work, for example, I want to do some checks in this form, I want tmb that when you click the add button add the information in certain variables , for later when you click the register button it sends this information to the comic and etc... I’m learning yet, but I thought it would be similar to c# for desktop

  • Well, if what you want to do is via . NET, you can take a look at that link in the microsoft forum, I believe that there you will find a way to solve your problem. NOTE: If this answer helped you, mark it as correct :D . Hugs!

Browser other questions tagged

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