使用 Struts2 + EasyUI DataGrid,點擊 add 動態新增行,後台定義全域變數 code 但表單提交後無法接收任何值。
<table id="dg" class="easyui-datagrid">
<thead><tr>
<th data-options="field:'code'">編碼</th>
<th data-options="field:'name'">名稱</th>
<th data-options="field:'price'">價格</th>
</tr></thead>
<tbody>...</tbody>
</table>
<a class="easyui-linkbutton" onclick="add()">add</a>
<script>
function add(){
$('#dg').datagrid('appendRow',{ code:'003', name:'name3', price:'300' });
}
</script>
getRows 或 getChanges 取得後,手動組裝表單或 AJAX 提交。rows[0].code、rows[1].code。關鍵點:動態新增的行屬於客戶端狀態,必須在提交前序列化為表單欄位或 JSON 發送。