是否可能:javascript 从 c:forEach 标签中提取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4506816/
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
Is possible : javascript extract value from c:forEach tag?
提问by Eswar
i have populate some values using c:forEach tag. I want to get those values in my javascript.
If I click GetCtagvalue button, then i want to read from (c:forEach) values in javascript.
我使用 c:forEach 标签填充了一些值。我想在我的 javascript 中获取这些值。
如果我单击GetCtag值按钮,那么我想从javascript 中的( c:forEach) 值中读取。
Is any other-way to retrieve the c:forEach tag value
是否有任何其他方式来检索 c:forEach 标记值
<%@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.
帮我。提前致谢。
回答by BalusC
Just print it in JavaScript syntax instead of HTML syntax.
只需用 JavaScript 语法而不是 HTML 语法打印它。
<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 namereturns Stringand agereturns Number):
以便它最终成为有效的 JavaScript 对象(假设name返回String并age返回Number):
<script>
var data = {
'foo': 10,
'bar': 5,
'waa': 20
};
</script>
回答by Eswar
It is only possible for JavaScript to access the data in the HTML as it is SEEN BY THE BROWSER.
JavaScript 只能访问 HTML 中的数据,因为它是由浏览器看到的。
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).
我推荐的方法是生成 JSON(但是您的 Web-API 允许)并将其存储在 JavaScript 代码中——可能是一个全局的。当然,建议将这些数据“更接近”使用它的位置,但同样如此。另一种方法是使用自定义 HTML 属性(希望从“data-”开始以符合 HTML5 标准)。
回答by Eswar
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);
}
}
回答by Chinthaka Dinadasa
<script>
var data = new Array();
<c:forEach items="${cTagBean.tagList}" var="ctag" varStatus="loop">
data.push(${ctag.name});
</c:forEach>
</script>

