如何从 <c:if 块调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12577482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:34:01  来源:igfitidea点击:

How to call a javascript function from a <c:if block

javascriptjsp

提问by user179516

I want to call a javascript function when flow enters into <c:if>block.

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

For example, I have below code:

例如,我有以下代码:

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

I need to invoke a JS function only when this <c:if>is true.

只有在这种<c:if>情况下,我才需要调用 JS 函数。

Please suggest how to achieve this.

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

回答by Prakash K

You can include the <script>tag within the <c:if>block, something like this:

您可以<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>

Hope this helps.

希望这可以帮助。