Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the submit button in a specific form in jQuery

I try to get jQuery object of a submit button in a specific form (there are several forms on the same page).

I managed to get the form element itself. It looks something like this:

var curForm = curElement.parents("form");

The current Element has the context HTMLInputElement. The several techniques I tried to get the according submit element of the form:

var curSubmit = curForm.find("input[type='submit']");
var curSubmit = $(curForm).find("input[type='submit']");    
var curSubmit = curForm.find(":submit");
var curSubmit = $(curForm).find(":submit");    
var curSubmit = $(curSubmit, "input[type='submit']");

the result is always the same (and very strange). The result that I get is the same element as "curElement".

So how can I get the right submit button?

like image 760
leifg Avatar asked May 23 '10 17:05

leifg


People also ask

How do you know which button is clicked on form submit?

In the form 'submit' event, you don't know what button (or if any button) was clicked. In general, you use the 'submit' event when you want to do stuff after the form is submitted, like validate fields, which is usually what you want.

How do you check form is submitted or not in jQuery?

$("#something-%"). submit(function(e) { e. preventDefault(); $("#some-span"). html(data); });


2 Answers

The following should work:

var submit = curElement.closest('form').find(':submit');
like image 157
SLaks Avatar answered Oct 23 '22 11:10

SLaks


This should work:

var curSubmit = $("input[type=submit]",curForm);

EDIT: Note the missing ' in the selector

like image 30
jigfox Avatar answered Oct 23 '22 13:10

jigfox