我有一个 int 变量,它从 java 类传递到 jsp 文件中。我现在想将此 int 值从 jsp 传递到我拥有的 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:
<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>
我只想将“products_per_page”值传递给 javascript。应该很简单...
最佳答案
你可以把你的<c:set>
将 block 作为其值放入 HTML 元素中,然后使用 Javascript 或 jQuery 轻松获取该值
例子:
<input id="ppp" type="text" value="<c:set var='products_per_page' value='3' />"/>
js:
var products_per_page = document.getElementById("ppp").value;
关于java - 将变量从 JSP 传递到 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748242/