Wednesday, August 13, 2014

Submit a form with different actions using Jquery.


Here is the jsp:

<form:form id="clientLinkAbuseTypesForm" method="post"  commandName="clientLinkAbuseTypeForm">
...

<input type="submit" id="saveLinkAbuse" value="Save">
<input type="submit" id="cancelLinkAbuse"  value="Cancel" >

</form:form>

Here I have 2 different buttons each need to be calls different action and also need to send all form parameters. In this situation we need to change the form action's dynamically

Here I am explaining with the Jquery:

<script language="javascript">
$(document).ready(function()
{
   $("#saveLinkAbuse").click(function()
   {
        $("#clientLinkAbuseTypesForm").attr("action", "saveClientLinkAbuseTypes");
      // we can change the method type
     like $("#clientLinkAbuseTypesForm").attr("method", "get");  // or post here.
       $("#clientLinkAbuseTypesForm").submit();

   });

                 $("#cancelLinkAbuse").click(function()
    {
  
     $("#clientLinkAbuseTypesForm").attr("action", "cancelLinkAbuse");
        $("#clientLinkAbuseTypesForm").submit();

    });
}
);
</script>

From the above code you can observe the action attribute changing dynamically.. This will hit the corresponding action methods in controller with form fields...

No comments: