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

前端 未结 4 1949
无人共我
无人共我 2020-12-18 11:57


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

相关标签:
4条回答
  • 2020-12-18 12:10
    <script>
            var data = new Array();     
            <c:forEach items="${cTagBean.tagList}" var="ctag" varStatus="loop">
                data.push(${ctag.name});        
            </c:forEach>
    </script>
    
    0 讨论(0)
  • 2020-12-18 12:14

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 12:16

    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).

    0 讨论(0)
  • 2020-12-18 12:18

    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>
    
    0 讨论(0)
提交回复
热议问题