将变量从 JSP 传递到 javascript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24748242/
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
Pass variable from JSP to javascript file
提问by user3777012
I have an int variable in that gets passed into a jsp file from a java class. I now want to pass this int value from the jsp to a javascript file I have. I am stuck with a coming up with a solution. I have tried the following but still does not work:
我有一个 int 变量,它从一个 java 类传递到一个 jsp 文件中。我现在想将这个 int 值从 jsp 传递给我拥有的 javascript 文件。我被困在想出一个解决方案。我尝试了以下但仍然无法正常工作:
javascript:
javascript:
jQuery(document).ready(function() {
jQuery('div.awsmp-hero-featureSwap').each(function() {
jQuery(this).find('div.hero-featureSwap').each(function() {
var width = 0;
var height = 0;
var count = 1;
var perpage = "${products_per_page}"; // ***** this is the value I want from the jsp*****
var totalcarousel = jQuery('.hero').length;
jQuery(this).find('div').each(function() {
width += jQuery(this).width();
count = count + 1;
if (jQuery(this).height() > height) {
height = jQuery(this).height();
}
});
jQuery(this).width(((width / count) * perpage + 1) + (perpage * 12) + 60 + 'px');
jQuery(this).height(height + 'px');
});
});
JSP:
JSP:
<c:set var="products_per_page" value="3" />
<c:if test="${not null model.listSize}">
<c:set var="products_per_page" value="${model.listSize}" />
</c:if>
I just want to pass the the "products_per_page" value to the javascript. should be very simple...
我只想将“products_per_page”值传递给 javascript。应该很简单...
回答by Yaje
you can put your <c:set>
block into an HTML element as its value then get the value easily using Javascript or jQuery
您可以将<c:set>
块放入 HTML 元素作为其值,然后使用 Javascript 或 jQuery 轻松获取该值
example :
例子 :
<input id="ppp" type="text" value="<c:set var='products_per_page' value='3' />"/>
js:
js:
var products_per_page = document.getElementById("ppp").value;
回答by Analyst
You can use this, is not very recommendable.... but works.. Hard-coding the value into the html file like this:
您可以使用它,不是很值得推荐....但有效..将值硬编码到 html 文件中,如下所示:
<body>
.....
var x = <%= some_value %>;
.....
</body>
回答by Rafa?
I passed the request argument to my onclick handler in js the following way:
我通过以下方式将请求参数传递给我在 js 中的 onclick 处理程序:
<s:submit name="kurierButton" onclick="openKurierWindow('%{#attr.OUTBOUND_KURIER_URL}');" value="%{#attr.OUTBOUND_KURIER}"/>
回答by Yash P Shah
Try doing it this way->
尝试这样做 - >
servlet_file.java
servlet_file.java
HttpSession session = request.getSession();
session.setAttribute("idList", idList);
.jsp file
.jsp 文件
<%
HttpSession session1 = request.getSession();
ArrayList<Integer> idList = (ArrayList<Integer>) session1.getAttribute("idList");
%>
.js file
.js 文件
alert("${idList}");