Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better jQuery solution to this.form.submit();?

I want to trigger the submit event of the form the current element is in. A method I know works sometimes is:

this.form.submit(); 

I'm wondering if there is a better solution, possibly using jQuery, as I'm not 100% sure method works in every browser.

Edit:

The situation I have is, as follows:

<form method="get">     <p><label>Field Label         <select onchange="this.form.submit();">             <option value="blah">Blah</option>             ....         </select></label>     </p> </form> 

I want to be able to submit the form on change of the <select>.

What I'm looking for is a solution that works on any field within any form without knowing the id or name on the form. $('form:first') and $('form') won't work because the form could be the third on the page. Also, I am using jQuery on the site already, so using a bit of jQuery is not a big deal.

So, is there a way to have jQuery retrieve the form the input/select/textarea is in?

like image 666
Darryl Hein Avatar asked Jan 06 '09 20:01

Darryl Hein


People also ask

Is jQuery submit deprecated?

No, it's not deprecated! Deprecated = Not current. Obsolete = no longer available. IMHO the reason is because as browsers/html standards add more events, the team doesn't want to keep adding aliases when .

How can we submit a form using jQuery?

jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.

How check File submit or not in jQuery?

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

Can I use instead of jQuery?

Nevertheless, Native javascript is one of the best jQuery alternatives, in fact, It is the framework of JS. Javascript is the best because any browsers ships with javascript and you do not need to install jQuery in your application.


1 Answers

I think what you are looking for is something like this:

$(field).closest("form").submit(); 

For example, to handle the onchange event, you would have this:

$(select your fields here).change(function() {     $(this).closest("form").submit(); }); 

If, for some reason you aren't using jQuery 1.3 or above, you can call parents instead of closest.

like image 173
Matthew Crumley Avatar answered Oct 02 '22 17:10

Matthew Crumley