tags:

views:

70

answers:

3

I have a btn

<a href="#" id="submitBtn" class="btnGray">Upload Docs</a>

and a jquery code to submit the form.

$("#docSaveBtn").click(function(){
  $("#docForm").submit();                           
});

I have multiple forms on the same page and I want to send the form id to submit which will submit the desired form when click of button with class 'btnGray'.

Your help is highly appreciated.

A: 

$(".btnGray").click(function(){ $("#docForm").submit();});

Chuck Vose
I am not sure if this would help. In this case I would have to write this for each form. I guess I should rephrase my question. I want to submit a particular form based on the form Id I send onClick of a button.
Aanu
+3  A: 

Proximity-based Submission...

A much cleaner solution would be proximity-based, where a link submits it's closest form:

$(".submitBtn").click(function(e){
  e.preventDefault();
  $(this).closest(".linkFormBlock").find("form").submit();
});

--

<div class="linkFormBlock">
  <a href="submitBtn">Upload Docs</a>
  <form>
    <input type="text" name="specialValues" />
  </form>
</div>

RELationships?

Another solution could be to use the rel attribute of the link to signify it's corresponding form:

$(".submitBtn").click(function(e){
  e.preventDefault();
  var id = $(this).attr("rel");
  $("#"+id).submit();
});

--

<a rel="myForm" class="submitBtn">Upload Docs</a>
<form id="myForm">
  <input type="text" name="specialValues" />
</form>
Jonathan Sampson
@Jonathan, Thanks!. It worked. I used the rel - solution. Appreciate your help.
Aanu
A: 

If you want to use the submit() method, you could just include any relevant parameters in your action attribute of your form tag. e.g:

<form action="/formProcessor.aspx?id=1234">
....
    <a href="#" id="submitBtn" class="btnGray">Upload Docs</a>
....
</form>

Using the jQuery you supplied in your question, this will automatically submit your form using the action designated in your form tag.

Jason