java - 将变量从 JSP 传递到 javascript 文件

标签 java javascript jquery jsp

我有一个 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/

相关文章:

使用 fadeIn + ReplaceWith 的 jQuery AJAX

javascript - Ember 使用 jquery 文件上传

具有索引、键和值的 Java 集合

javascript - Ng-Show 和 Ng-Hide,有什么区别?

java - forEach() 方法如何在 Java 8 中工作

javascript - 在图像上定位图像

javascript - JQuery 选择和交换样式表 href 值

javascript - jQuery 函数永远不会在 .ready() 上执行

java - 如何让程序在等待事件时保持打开状态

java - 在列表中加载静态图像会减慢滚动速度