2

当流进入<c:if>块时,我想调用一个 javascript 函数。

例如,我有以下代码:

<c:if test="${!empty errorMessage && !(fn:contains(errorMessage, 'generic'))}">
    <span> errorError occured<br />
    </span>
</c:if>

<c:if>只有当这是真的时,我才需要调用 JS 函数。

请建议如何实现这一目标。


您可以<script>在块中包含标签<c:if>,如下所示:

<c:if test="${!empty errorMessage && !(fn:contains(errorMessage, 'generic'))}">
    <span>
        errorError occured<br />
    </span>
    <script>
        alert("Calling my function ...");
        myFunction(); // this is a call to your function which must be defined in the <head> section or another JS file.
    </script>
</c:if>

希望这可以帮助。

4

1 回答 1

6

您可以<script>在块中包含标签<c:if>,如下所示:

<c:if test="${!empty errorMessage && !(fn:contains(errorMessage, 'generic'))}">
    <span>
        errorError occured<br />
    </span>
    <script>
        alert("Calling my function ...");
        myFunction(); // this is a call to your function which must be defined in the <head> section or another JS file.
    </script>
</c:if>

希望这可以帮助。

于 2012-09-25T07:14:49.483 回答