2017-01-09 92 views
-1

我有一個基於Spring Web模型 - 視圖 - 控制器(MVC)框架的項目。 Spring Web模型 - 視圖 - 控制器(MVC)框架的版本是3.2.8。 ,我想更改提交按鈕上的表單操作。所以這是我的代碼,但根本不會改變任何東西!Jquery在提交時更改表單操作

<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> 

我也試過

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

但即使是警報顯示不出來

+0

沒有ü嘗試刪除'TYPE =」從'button'標籤提交''屬性? –

+1

可能的重複 - http://stackoverflow.com/questions/14375144/jquery-prevent-default-then-continue-default –

+1

您的代碼有效。 https://jsfiddle.net/qc1fo7wq/ –

回答

1

你只需要event.preventDefault().submitread-more

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