How to call javascript function from tag lib's?

我们两清 提交于 2020-01-06 14:07:00

问题


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

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>

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

55.959090909090904
118.94545454545454
133.46818181818182
19.727272727272727

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

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

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

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

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


回答1:


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>


来源:https://stackoverflow.com/questions/28559648/how-to-call-javascript-function-from-tag-libs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!