Check type with jquery

Asked

Viewed 50 times

2

Well I have two buttons on a form, being them:

<button type='reset' onclick="history.back()">CANCELAR</button>
<button type='submit'>ok</button>

I have the following jquery that identifies clicking the button and sending the form:

$(document).ready(function () {
    $("button").click(function () {

            // Envia formulário
            $("form").submit();

    });
});

I need the $("form").submit(); password executed only when the clicked button is type='submit'.

it is possible to do this with jquery?

  • A button type='submit' already does form Ubmit. It seems to me that you are using jQuery without need? or this is a reduced version of your code?

  • This in a reduced version.

2 answers

3


Use a attribute selector:

$("button[type=submit]").click(function () {
   $("form").submit();
});
  • Just one more question, how to submit only the form that the button corresponds to? I have some page that has more than one form, it is possible to make the button identify which form it is and send it?

  • @Hugoborges by default a button of type submit already submits the form you are subscribed to. However if you want to do this by jquery, you can do thus, using the method closest.

  • for you to understand better what I am doing I will post the most complete code, https://jsfiddle.net/t5wmdkwp/5/ . I’m trying to get the button disabled for 2 seconds. however prop('disabled', true) prevents sending the form, I’ll deal with the $("form").submit() only the button sends only the ulmo form and he has to send the form that belongs to the button. Better understood my problem?

  • Good the closest. solved the problem. Look how it turned out. https://jsfiddle.net/t5wmdkwp/9/ Thank you again.

0

I believe by changing the type of the button reset for button is enough, the code would look like this:

<type='button' onclick="history.back()">CANCELAR</button>
  • 1

    I think you misunderstood the question.

Browser other questions tagged

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