我需要帮助,以便当用户编辑在一个框中键入的值时,它会更新另一个框中的值。例如,当用户更新红色字段时,我希望橙色字段自动更新以反映红色字段的值。我已经包含了一个屏幕截图来演示以及我正在使用的代码。我考虑过只使用一些 javascript ontab,但我没有看到任何像我这样的动态字段名称的示例。在代码中的字段
'<%="lnEntryItem[" + longNameIndex + "].start_month"%>' , '<%="lnEntryItem[" + longNameIndex + "].start_day"%>' and '<%="lnEntryItem[" + longNameIndex + "].start_year"%>'` should update `'<%="lnEntryItem[" + longNameIndex + "].end_month"%>' , '<%="lnEntryItem[" + longNameIndex + "].end_day"%>' and '<%="lnEntryItem[" + longNameIndex + "].end_year"%>'` respectively.
代码:
<%!
private String current(int index) {
String output = "";
if (index == 0) {
output = "<td> <font color=\"red\"><em>Current</em></font></td>";
} else if (index == 1) {
output = "<td> <font color=\"blue\"><em>Next Most Recent</em></font></td>";
} else if (index == 2) {
output = "<td> <font color=\"blue\"><em>2nd Most Recent</em></font></td>";
} else if (index == 3) {
output = "<td> <font color=\"blue\"><em>3rd Most Recent</em></font></td>";
} else if (index >= 4) {
output = "<td> <font color=\"blue\"><em>" + index + "th Most Recent</em></font></td>";
}
return output;
}
%>
<html:form action="EditUnit.do" method="POST" enctype="multipart/form-data" focus="xid">
<p><bean:define id="oldForm" property="writeUnitForm" name="ses" scope="session"/>
<bean:define id="newForm" name="EditUnitForm" scope="session"/>
<bean:define id="unit_id" property="unit_id" name="oldForm" scope="page"/>
<bean:define id="unit" property='<%= (String) pageContext.getAttribute("unit_id")%>' name="listUnitInfo" scope="page"/>
Unit ID: <bean:write property="unit_num" name="unit" scope="page"/></p>
<hr/>
Unit names: Enter Names and start date (default: today)
<table border="0" cellspacing="0">
<tr style="border-bottom: #000000 1px solid;">
<th>Start (mm/dd/yyyy) </th>
<th> - </th>
<th> End (mm/dd/yyyy) </th>
<th>Long Name</th>
<th> </th>
<th> </th>
</tr>
<%! int longNameIndex;%>
<%! int longNameCount = 0;%>
<logic:iterate id="lnEntryItem" indexId="longNameIndex" property="long_names" name="newForm" scope="page">
<tr>
<td style="text-align: right;"><html:text property='<%="lnEntryItem[" + longNameIndex + "].start_month"%>' size="2" maxlength="2"/>/<html:text property='<%="lnEntryItem[" + longNameIndex + "].start_day"%>' size="2" maxlength="2"/>/<html:text property='<%="lnEntryItem[" + longNameIndex + "].start_year"%>' size="4" maxlength="4"/> </td>
<td> - </td>
<% if (longNameIndex == 0) {%>
<td style="text-align: right;"><html:text property='<%="lnEntryItem[" + longNameIndex + "].end_month"%>' size="2" maxlength="2"/>/<html:text property='<%="lnEntryItem[" + longNameIndex + "].end_day"%>' size="2" maxlength="2"/>/<html:text property='<%="lnEntryItem[" + longNameIndex + "].end_year"%>' size="4" maxlength="4"/> </td>
<% } else {%>
<td style="text-align: right;"><html:text readonly="true" property='<%="lnEntryItem[" + longNameIndex + "].end_month"%>' size="2" maxlength="2"/>/<html:text readonly="true" property='<%="lnEntryItem[" + longNameIndex + "].end_day"%>' size="2" maxlength="2"/>/<html:text readonly="true" property='<%="lnEntryItem[" + longNameIndex + "].end_year"%>' size="4" maxlength="4"/> </td>
<% }%>
<td> <html:text property='<%="lnEntryItem[" + longNameIndex + "].data"%>' size="70" maxlength="70"/> </td>
<%=current(longNameIndex)%>
<% if (longNameIndex == 0) {%>
<td><font color="red"><strong>*Required</strong></font></td>
<% } else {%>
<td></td>
<% }%>
</tr>
<%longNameCount++;%>
</logic:iterate>
<tr>
<td></td>
<td></td>
<td></td>
<td><input type="submit" name="btn_addfield_longName" value="Add a Long Name"/>
<% if (longNameCount > 1) {%>
<input type="submit" name="btn_rmvfield_longName" value="Remove Last Row"/>
<% }%>
<%longNameCount = 0;%>
</td>
<td></td>
</tr>
</table>
我试图使用的 JQuery 脚本:
<script>
$(function(){
$('START DATE ID').keyup(function() { //how to know what that is?
var item=$(this);
if(item.val() != "") {
var tot = item.val();
$('END DATE ID').val(tot); //how to know what that is?
}
});
});
</script>
但我不确定如何修改它以合并动态字段名称。
这是jsp的HTML:
<table border="0" cellspacing="0">
<tbody>
<tr style="border-bottom: #000000 1px solid;">
<th>Start (mm/dd/yyyy) </th>
<th> - </th>
<th> End (mm/dd/yyyy) </th>
<th>Long Name</th>
<th> </th>
<th> </th>
</tr>
<tr>
<input type="hidden" name="lnEntryItem[0].id" value="2750"/>
<td style="text-align: right;">
<input type="text" name="lnEntryItem[0].start_month" maxlength="2" size="2" value="05"/>
/<input type="text" name="lnEntryItem[0].start_day" maxlength="2" size="2" value="30"/>
/<input type="text" name="lnEntryItem[0].start_year" maxlength="4" size="4" value="2013"/>
</td>
<td> - </td>
<td style="text-align: right;">
<input type="text" name="lnEntryItem[0].end_month" maxlength="2" size="2" value=""/>
/<input type="text" name="lnEntryItem[0].end_day" maxlength="2" size="2" value=""/>/
<input type="text" name="lnEntryItem[0].end_year" maxlength="4" size="4" value=""/>
</td>
<td>
<input type="text" name="lnEntryItem[0].data" maxlength="70" size="70" value="Jen's test unit"/></td>
<td> <font color="red"><em>Current</em></font></td>
<td><font color="red"><strong>*Required</strong></font></td>
</tr>
<tr>
<input type="hidden" name="lnEntryItem[1].id" value="2757"/>
<td style="text-align: right;"><input type="text" name="lnEntryItem[1].start_month" maxlength="2" size="2" value="05"/>
/<input type="text" name="lnEntryItem[1].start_day" maxlength="2" size="2" value="01"/>
/<input type="text" name="lnEntryItem[1].start_year" maxlength="4" size="4" value="2013"/>
</td>
<td> - </td>
<td style="text-align: right;">
<input type="text" name="lnEntryItem[1].end_month" maxlength="2" size="2" value="05" readonly="readonly"/>
/<input type="text" name="lnEntryItem[1].end_day" maxlength="2" size="2" value="30" readonly="readonly"/>
/<input type="text" name="lnEntryItem[1].end_year" maxlength="4" size="4" value="2013" readonly="readonly"/>
</td>
<td>&
<input type="text" name="lnEntryItem[1].data" maxlength="70" size="70" value="Jen's testing unit"/></td>
<td><font color="blue"><em>Next Most Recent</em></font></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>
<input type="submit" name="btn_addfield_longName" value="Add a Long Name"/>
<input type="submit" name="btn_rmvfield_longName" value="Remove Last Row"/>
</td>
<td></td>
</tr>
</tbody>
</table>