Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form is submitted when I click on the button in form. How to avoid this?

I use twitter-boostrap and I'd like to use these radio-buttons in my form. The problem is when I click on any of these buttons, the form is immediately submitted. How to avoid this? I just want to use default buttons like radio-buttons.

from:

<%= form_for @product do |f| %>
    <div class="control-group">
      <%= f.label :type, :class => 'control-label' %>

      <div class="controls">
        <div class="btn-group" data-toggle="buttons-radio">
          <button class="btn">Button_1</button>
          <button class="btn">Button_2</button>
        </div>
      </div>

    </div>

    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', products_path, :class => 'btn' %>
    </div>
<% end %>

javascript:

// application.js
$('.tabs').button();
like image 670
evfwcqcg Avatar asked Mar 10 '12 03:03

evfwcqcg


People also ask

How do I stop a form from automatically submitting?

You will have to set the type to button. But if you bind your event handler like below, you target all buttons and do not have to do it manually for each button! It's e. preventDefault(); .

How do you stop re submitting a form after clicking back button?

You can check if the user clicked the back button, disable form if true. Another way is by storing a cookie which you check on page load, if it exists you can disable the form.

How do I turn off submit button in Google forms?

There is not an option to remove the Submit Button; however, you can choose to hide it on the Form if you'd like, making the "Form" an informational web page/landing page rather than a page that requires/expects action. To do this, you will need to add some CSS code to a Custom Theme. Don't worry, it's easy!


1 Answers

From the fine HTML5 specification:

A button element with no type attribute specified represents the same thing as a button element with its type attribute set to "submit".

And a <button type="submit"> submits the form rather than behaving like a simple <button type="button"> push-button.

The HTML4 spec says the same thing:

type = submit|button|reset [CI]
This attribute declares the type of the button. Possible values:

  • submit: Creates a submit button. This is the default value.
  • reset: Creates a reset button.
  • button: Creates a push button.

So your <button> elements:

<button class="btn">Button_1</button> <button class="btn">Button_2</button> 

are the same as these (in compliant browsers):

<button type="submit" class="btn">Button_1</button> <button type="submit" class="btn">Button_2</button> 

and any time you hit one of those buttons you'll submit your form.

The solution is to use plain buttons:

<button type="button" class="btn">Button_1</button> <button type="button" class="btn">Button_2</button> 

Some versions of IE default to type="button" despite what the standard says. You should always specify the type attribute when using a <button> just to be sure that you will get the behavior you're expecting.

like image 195
mu is too short Avatar answered Sep 23 '22 08:09

mu is too short