-1

我在 Struts 2 中有一个动态表。我想在其中将迭代器索引值传递给 Javascript 函数。我怎样才能做到这一点?我已经给出了下面的代码。

这是我的桌子:

<table class="tblborder" id="priorIncident">
        <thead>
            <tr bgcolor="gray">
                <th colspan="5" align="center"><s:text name="priorIncidentTab" /></th>
            </tr>
            <tr bgcolor="black">
                <th><s:text name="date" /></th>
                <th><s:text name="reason" /></th>
                <th><s:text name="warningInd" /></th>
                <th><s:text name="warningDate" /></th>
                <th><s:text name="warningDesc" /></th>
            </tr>
        </thead>
        <tbody>
        <s:iterator value="claimData.priorIncidents" var="priorIncident" status="status">
            <tr>
                <s:hidden name="claimData.priorIncidents[%{#status.index}].oid" />
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].incidentDate" id="incidentDate1"/></td>
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].incidentReason" id="incidentReason1"/></td>
                <td>
                    <s:select headerKey="" headerValue="Select" list="#{'Y':'Yes', 'N':'No'}" theme="simple" name="claimData.priorIncidents[%{#status.index}].hasWarning" id="hasWarning1"/>
                </td>
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].warningDate" id="warningDate1"/></td>
                <td><s:textfield theme="simple" name="claimData.priorIncidents[%{#status.index}].warningDesc" id="warningDesc1"/></td>
            </tr>
        </s:iterator>
        </tbody>
    </table>
    <td><a href="#" id="add" onclick="MyFunction(%{#status.index})">Add</a></td>
    <td><a href="#" id="delete">Delete</a></td>

当我单击添加链接(不在迭代器内)时,我应该能够将索引值传递给一些 Javascript 函数吗?由于我是 JS 新手,有人可以帮忙吗?任何答案都非常感谢?

4

3 回答 3

1

缺少单引号......这必须有效

<a href="#" id="add" onclick="MyFunction('%{#status.index}')">Add</a></td>

如果不尝试如下

<a href="#" id="add" onclick="MyFunction('<s:property value="%{[#stat.index]}"/>')"> Add</a></td>
于 2013-04-25T13:03:53.867 回答
0

状态范围在迭代器内部。使用标准 c 标签库将 status.index 的值设置为页面范围变量。

<c:set var="foo" scope="page" value="..."/>  

顺便说一句, status.index 的最后一个值始终是数组 -1 的大小。

于 2013-04-19T09:08:38.843 回答
0

目前还不清楚你到底要什么。

您可以在渲染时使用列表的大小初始化 JS 变量。MyFunction不过,需要增加该值,以便您可以添加行;对函数调用中的值进行硬编码将不起作用。

这意味着硬编码方法调用中的值将不起作用:您需要使用 JS 变量。

<s:set>如果您的目标是有一个在循环结束时开始计数的“添加”链接,您还可以在迭代器块中添加一个变量来增加计数器,或者在最后一次迭代中设置一个变量。

最终,由于您需要在添加更多项目时不断更新该值,因此简单地设置一个具有列表大小的 JS 变量似乎是最佳选择。如果您想要每个项目的“添加”链接,那么您可以使用迭代块的索引。

于 2013-04-16T15:30:01.167 回答