Search code examples
jqueryajaxformsstruts2struts2-jquery

Struts <s:submit> and Struts-jquery <sj:submit> will not work in same form Part 2


This is in reference to question JQuery Ajax form submit along with usual submit on the same form in Struts 2.

I am also trying to get a regular Struts 2 <s:submit> and a Struts 2 jQuery <sj:submit> to work on the same page.

I followed the answer that Roman C gave, but it still doesn't work.

My setup for the form looks like this:

<head>
    <sj:head/>
</head>
<s:form id="dataForm" action="updateData" method="POST">
    <s:hidden name="updatedDate" />
    <s:submit id="nonAjaxSubmit" name="updateDataSubmit" value="Update and Close"/>
</s:form>
<sj:submit formIds="dataForm" name="updateDataAjaxSubmit" value="Update" targets="result"/>

I have the <s:submit> inside the form as Roman C specified.

The Ajax Submit <sj:submit> button works, but the regular submit <s:submit> button that's inside the form doesn't work.

I'm not trying to override the action method to use like the other questioner, I'm just trying to get the <s:submit> to work.

I need the <s:submit> to trigger the action and do the redirect as specified by the action in the struts.xml file. And I need the <sj:submit> to trigger the same action asynchronously while leaving the loaded page in the browser alone.

If anyone has an idea of what I might need to do to get it to work, that would be helpful.


Solution

  • If the plugin overrides a submit event on the form then you should try click event and submit the form using javascript submit().

    For example:

    <script type="text/javascript">
     $(document).ready(function(){
       $("nonAjaxSubmit").click(function(e){
         e.preventDefault();
         this.form.submit();
       });
     });
    </script>