【问题标题】:How to pass an iterator index value to Javascript in struts 2?如何在struts 2中将迭代器索引值传递给Javascript?
【发布时间】:2013-04-16 15:15:43
【问题描述】:

我在 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 新手,有人可以帮忙吗?任何答案都非常感谢?

【问题讨论】:

    标签: javascript jquery html-table struts2 iterator


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        不清楚你到底要什么。

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

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

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

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

        【讨论】:

        • 普遍赞赏不赞成票的解释。答案几乎总结了您的选择。
        猜你喜欢
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 2015-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多