2017-05-04 106 views
1

我有形式,帶彈簧的jsp:input元素綁定:Spring MVC中添加一行形式

<table id="tab_logic"> 
<thead></thead> 
<tbody> 
<tr id='addr0'> 
    <td>                     
     <spring:bind path="createForm.contractEntitlements[0].entitledQuantity">                    
      <form:input type="number" min="0" max="999" name="entitled_quantity" id="entitled_quantity" path="${status.expression}"/>                  
     </spring:bind>                   
    </td> 
</tr> 
<tr id='addr1'></tr> 
</tbody> 
</table> 
<input type="button" id="add_row" value="show" /> 
<input type="button" id="delete_row" value="hide" /> 

我需要它是dyanmic所以我增加了一個按鈕來添加/刪除一行,並調用腳本點擊。爲了進行測試,看看它是否會增加或不我只是作爲一個標籤:

$(document).ready(function(){ 
    var i=1; 
    $("#add_row").click(function(){ 
    $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='"+i+"' type='text' /></td>"); 

    $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>'); 
    i++; 
}); 
    $("#delete_row").click(function(){ 
    if(i>1){ 
     $("#addr"+(i-1)).html(''); 
     i--; 
     } 
    }); 

}); 

它增加並相應刪除的行,但是當我行更改爲:發生

$('#addr'+i).html("<td>"+ (i+1) +"</td><td><form:input name='"+i+"' type='text' path='createForm.contractEntitlements[1].category' /></td>"); 

沒事的時候我點擊按鈕,日誌上沒有錯誤。彈簧綁定發生在頁面加載並且無法再綁定或者我做錯了嗎?

回答

1

您可以添加添加獨立的功能和刪除

function add(){ 
 
    var row = $("#tab_logic > tbody > tr:first").html(); //You can create your own html here 
 
    $('#tab_logic > tbody ').append('<tr>'+row+'</tr>'); 
 
    
 
} 
 

 
function removeElm(obj){ 
 
     var row = $('#tab_logic > tbody > tr').length; 
 
    
 
    if(row <= 1){ 
 
     alert("Not possible to delete this row"); 
 
     return; 
 
    } 
 
    
 
    $(obj).parent().parent().remove(); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="tab_logic"> 
 
    <tr id="tr1">   
 
     <td> <input type="text"/> </td> 
 
     <td> <label onclick="removeElm(this)">Delete</label> </td> 
 
    </tr> 
 
</table> 
 
<input type="button" onclick="add()" value="add me"/>