如何从标签库调用JavaScript函数? [英] How to call javascript function from tag lib's?

查看:278
本文介绍了如何从标签库调用JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个关于javascript的问题,如何从jsp或jspx文件中调用js函数.

Well, I have a question regarding javascript, how to call a js function from jsp or jspx files.

在jspx页面上,我有一个循环,该循环将数据添加到表中,这是代码示例

on my jspx page I have a loop which adds data to a table, here is the code sample

<c:forEach items="${loadList}" var="load">
   <tr>
     <td>
       <nobr><c:out value="${load}"/></nobr>
     </td>
  </tr>
</c:forEach>

第3行在加载变量var上执行c:out,输出为

Here the line no 3 does a c:out on the load var which prints as

55.959090909090904
118.94545454545454
133.46818181818182
19.727272727272727

等值. 所以我写了一个js函数将这个值四舍五入为

etc values. So I have written a js function to round of this values to as

55.95
118.94
133.46
19.72

JS函数

function roundOfValue(value, limit) {
    value = value.toFixed(limit);
    return value;
}

我试图在c:out中以

I am trying to call this function in the c:out as

<nobr><c:out value="roundOfValue(${system},2);"/></nobr>

,但这不会调用该函数,并且会在UI上打印该函数.有没有一种方法可以调用此函数,该函数将打印四舍五入的值.

but this does not call the function and prints the same on the UI. Is there a way to call this function which will prints the rounded off values.

我什至尝试了以下方法,但是它不起作用

I even tried the following but it does not work

1. <c:forEach items="${systemList}" var="system" varStatus="row">
2.  <tr><td id="load">
3.   <nobr><div id="div"></div>
4.    <script>
5.     document.getElementById(div).innerHTML =  roundOfValue(${system},2);
 6.   </script></nobr>
4.  </td></tr>
5. </c:forEach>

请提出一些步骤. 谢谢

Please suggest some steps to do this. Thanks

推荐答案

使用JSTL代替JS:

Use JSTL instead of JS:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<nobr><fmt:formatNumber value="${load}" pattern="0.00"/></nobr>

这篇关于如何从标签库调用JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆