Translate

Thursday, August 2, 2012

jQuery - Submit a Form Using an Anchor Tag

While you can use JavaScript in the onclick event of an anchor tag to submit a form, there may be times when you need to submit additional information along with the form. That's what I needed to do and this is how I did it using the jQuery method. This example is for Struts 2 but should work with a regular anchor tag.


<s:url var="urlFkey" namespace="%{namespace}" action="GotoAddForeignKeyRecord" escapeAmp="false" >
  <s:param name="target" >disp${fieldName}</s:param>
  <s:param name="fkeyName" value="fkeyName"/>
  <s:param name="fkeyTableName" value="fkeyPrimaryTableName"/>
  <s:param name="recordID" value="recordID"/>
</s:url>
<s:a id="disp%{fieldName}" href="%{urlFkey}" cssClass='nyroModal' onclick="this.href = this.href + '&' + $('#%{dispTablename}saveRecord').serialize();">
   Add New <s:property value="fkeyPrimaryTableNameLabel"/>
</s:a>

In the JSP snippet above I am calling the jQuery serialize method in the onclick event of the anchor tag and appending the returned result to the href for the tag.

No comments:

Post a Comment

Thank you for commenting!