DOM中cloneNode的使用之旅
struts2中可以自动封装表单提交过来的参数
List<RuleHour> exSeasonOut = new ArrayList<RuleHour>();
<tr id="exSeasonOutTr"> <td> <sky:datePicker name="ruleVO.exSeasonOut[${status.index}].fromDate,ruleVO.exSeasonOut[${status.index}].toDate" /> </td></tr>
$("exSeason").on("click",addRow);/* copy the tr(id) and append to the table, change the id、name of the new tr at the same time */function addRow(id) { var tr = $(id);if(!tr.index) {tr.index = 0;}tr.index++;var newTr = tr.cloneNode(true);newTr.innerHTML = newTr.innerHTML.replace(/[\d]/g, tr.index);tr.parentElement.appendChild(newTr);}
newTr.innerHTML = newTr.innerHTML.replace(/[\d]/g, tr.index);
newTr.childNodes[0].innerHTML = newTr.childNodes[0].innerHTML.replace(/[\d]/g, tr.index);
var inputs = newTr.$T("input");for(var i=0;i<inputs.length;i++) { inputs[i].id = inputs[i].id.replace(/[\d]/g, tr.index); inputs[i].name = inputs[i].name.replace(/[\d]/g, tr.index);}
<fieldset> <legend> <div style="float:left;">季节</div> <div style="float:left;"> <input type="button" value="添加" onclick="addRow('seasonOutTr')"/></div&].innerHTML.replace(/[\d]/g, tr.index); var inputs = newTr.$T("input"); for(var i=0;i<inputs.length;i++) { inputs[i].id = inputs[i].id.replace(/[\d]/g, tr.index); inputs[i].name = inputs[i].name.replace(/[\d]/g, tr.index); if(inputs[i].id.indexOf("from") != -1) { inputs[i].onclick = function() { WdatePicker({ dateFmt:'yyyy-MM-dd', maxDate:'#F{$dp.$D(\'' + this.id.replace("from", "to") + '\')}' }); }; } else { inputs[i].onclick = function() { WdatePicker({ dateFmt:'yyyy-MM-dd', minDate:'#F{$dp.$D(\'' + this.id.replace("to", "from") + '\')}' }); }; } } tr.parentElement.appendChild(newTr); } function addCombineFaresRow(id) { var tr = $(id); if(!tr.index) { tr.index = 0; } tr.index++; var newTr = tr.cloneNode(true); var inputs = newTr.$T("input"); for(var i=0;i<inputs.length;i++) { inputs[i].id = inputs[i].id.replace(/[\d]/g, tr.index); inputs[i].name = inputs[i].name.replace(/[\d]/g, tr.index); if(inputs[i].id.indexOf("seqId") != -1) { inputs[i].value = tr.index+1; } } tr.parentElement.appendChild(newTr); } /* delete the table's last tr */ function deleteRow(id) { var tr = $(id); if(!tr.index || tr.index==0) { return; } var trs = tr.parentElement.childNodes; tr.parentElement.removeChild(trs[trs.length-1]); tr.index--; }function addTimeRow(id) { var table = $(id); var count = table.rows.length; var tr = table.insertRow(); var td = tr.insertCell(); var names = [id + "["+count+"].fromDate", id + "["+count+"].toDate"]; td.appendChild(Sky.createEl({ tag: "input", name: names[0], className: ["datePicker", "date"], onClick: function(){ WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\''+names[1]+'\')}'}); } })); td.appendChild(Sky.createText(" - ")); td.appendChild(Sky.createEl({ tag: "input", name: names[1], className: ["datePicker", "date"], onClick: function(){ WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\''+names[0]+'\')}'}); } })); }