【问题标题】:Enable / disable Submit button if the count in s:iterator value is great than 0如果 s:iterator 值中的计数大于 0,则启用/禁用提交按钮
【发布时间】:2014-12-16 15:33:02
【问题描述】:

我的 JSP 中有以下代码,我使用的是 Struts2

<s:iterator value="incentiveList" status="stat">
    <tr>
        <td><s:checkbox name="checkboxes[%{#stat.index}]" theme="simple" /></td>
        <td></td>
        <td><s:property value="marketingAmount" /> <s:hidden value="%{incentiveList.marketingAmount}" /></td>
        <td></td>
        <td></td>
        <td></td>
        <td><s:property value="marketingIncentive" /></td>
        <td></td>
        <td></td>
        <td></td>
        <td><s:property value="advertizingIncentive" /></td>
        <td></td>
        <td></td>
        <td><s:property value="channelPlacementIncentive" /></td>
        <td></td>
    </tr>
</s:iterator>
</table>
</br>
</br>
<s:if test="#status.index gt 0">
<s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()'/>
</s:if>
<s:else>
<s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()' disabled="true"/>
</s:else>

现在我想启用 submitButton 如果上面的激励列表有任何值,即 count gt 0 并且为了实现这一点,我在下面的代码中使用了

<s:if test="#status.index gt 0">
            <s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()'/>
            </s:if>
            <s:else>
            <s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()' disabled="true"/>
</s:else>

但是没有用,如有遗漏请指正

【问题讨论】:

    标签: list jsp struts2 ognl


    【解决方案1】:

    如果incentiveList 没有值,您将一无所获,而不仅仅是按钮。你最终会得到无效的&lt;table&gt;&lt;/table&gt; 代码,那么&lt;s:if&gt; 也应该放在迭代器周围:

    <s:if test="incentiveList.size()>0">
        <!-- iterator here -->
    
        <!-- buttons here -->
    </s:if>
    

    P.S: IteratorStatus 是iterator-scoped(和iterator-scoped),它不会存在于Iterator之外,而且它没有用,因为它携带有关当前迭代元素的信息

    【讨论】:

    • 谢谢安德里亚你怎么看java.sun.com/jsp/jstl/core" prefix="c"%>
    • 我应该怎么想... :) 这是另一种好方法,我更喜欢 Struts 标签和 OGNL,我在回答中使用了它们,因为您在问题中使用了它们 :) 顺便说一句,记住将 IF(无论您选择使用哪种类型)放在整个表格周围,或者使用迭代器外部的 元素创建一个 ,否则您将得到无效的 HTML
    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 2018-12-08
    • 2017-01-17
    • 2012-04-18
    • 2020-09-15
    • 2020-04-18
    • 1970-01-01
    • 2019-07-19
    相关资源
    最近更新 更多