3

I have a project based in Spring Web model-view-controller (MVC) framework. The version of the Spring Web model-view-controller (MVC) framework is 3.2.8. and I want to change the form action on a submit button. So here is my code but does not change anything at all !

<script type="text/javascript">
    $('#awardProductsButton').click(function(){
       $('#devicesFormId').attr('action', 'test');
    });
</script>

<form:form commandName="devicesForm" name="devicesForm" id="devicesFormId" method="post"     
action="${contextPath}/newdesign/manage/devices/${devicesForm.devices.id" htmlEscape="yes" enctype="multipart/form-data">   

<button id="awardProductsButton" class="btn btn-primary" type="submit">AWARD product/s</button>

 </form:form>

I also tried

$('#awardProductsButtonId').submit(function(event){
            alert ('test');
            event.preventDefault();
            $('#deviceFormId').attr('action', '${contextPath}/newdesign/manage/device/${deviceForm.device.id}');
        });

but even the alert does not show up

Amadeu Cabanilles
  • 913
  • 3
  • 19
  • 47

1 Answers1

5

you just need event.preventDefault() and .submit read-more

<script type="text/javascript">
    $('#devicesFormId').submit(function(event){
       event.preventDefault()
       $('#devicesFormId').attr('action', 'test');
       //$(this).attr('action', 'test');
    });
</script>
codenut
  • 683
  • 1
  • 7
  • 21
  • 2
    Just curious..why wouldn't this become an infinite loop? Wouldn't the form just keep resubmitting? – bos570 Aug 19 '18 at 14:22
  • 1
    Well the form doesn't submit at all now (due to `preventDefault`).. Only the action attribute is changed. There will be recursion if you also include `.submit()` indeed. – Melroy van den Berg Apr 21 '22 at 22:11