Is possible : javascript extract value from c:forEach tag?

亡梦爱人 提交于 2019-12-18 05:17:18

问题



i have populate some values using c:forEach tag. I want to get those values in my javascript.
If I click GetCtag value button, then i want to read from (c:forEach) values in javascript.

Is any other-way to retrieve the c:forEach tag value

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>    
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<f:view>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

       <script type="text/javascript"> 

       function getCTagValue(ctagObject)
       {
          alert("CFor Each Tag Object Value: " + ctagObject);
          // Here i want write code for retrieve the c:forEach tag value   
       }

</script>
</head>
    <body>
        <h:form id="cTagForm" >               

            <c:forEach items="${cTagBean.tagList}" var="ctag">
                <c:out value="${ctag.name} : "/>
                <c:out value="${ctag.age}"/></br>
            </c:forEach>

            <a4j:commandButton id="GetCtagId"  value="GetCtag" oncomplete="getCTagValue('#{cTagBean.tagList}')"/>

        </h:form>
    </body>
</html>

Help me. Thanks in advance.


回答1:


Just print it in JavaScript syntax instead of HTML syntax.

<script>
    var data = {
        <c:forEach items="${cTagBean.tagList}" var="ctag" varStatus="loop">
            '${ctag.name}': ${ctag.age}${!loop.last ? ',' : ''}
        </c:forEach>
    };
</script>

So that it end up as valid JavaScript object (assuming that name returns String and age returns Number):

<script>
    var data = {
        'foo': 10,
        'bar': 5,
        'waa': 20
    };
</script>



回答2:


It is only possible for JavaScript to access the data in the HTML as it is SEEN BY THE BROWSER.

The method I would recommend is to generate JSON (however your Web-API allows that) and store it in JavaScript code -- perhaps a global. Of course, keeping this data "closer" to where it is used is advisable, but the same holds. Another approach is to use custom HTML attributes (hopefully starting with "data-" for HTML5-compliance).




回答3:


I got the solution.

<a4j:commandButton id="GetCtagId" 
                   value="GetCtag" 
                   data="#{cTagBean.tagList}" 
                   oncomplete="getCTagValue(data)"/>

Then,

function getCTagValue(data) 
{                    
    for(var i=0; i<data.length; i++)
    {
        alert("Month : " + data[i].name);
        alert("Value : " + data[i].age);
    }
}



回答4:


<script>
        var data = new Array();     
        <c:forEach items="${cTagBean.tagList}" var="ctag" varStatus="loop">
            data.push(${ctag.name});        
        </c:forEach>
</script>


来源:https://stackoverflow.com/questions/4506816/is-possible-javascript-extract-value-from-cforeach-tag

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