【问题标题】:How to store a jquery variable in session/request/application in jsp?如何在jsp的会话/请求/应用程序中存储一个jquery变量?
【发布时间】:2012-10-05 07:17:02
【问题描述】:

我正在使用 jQuery Mobile 和 Spring MVC 开发一个包含多页模板的移动应用程序。我有一个包含锚链接的 jsp;每当我单击链接 Pagebeforechange 时,都会调用事件并执行 jQuery 函数。我从函数内的链接中获得了一个 id,并将其存储在一个 jQuery 变量中。现在我想重用 jstl if 标签中的 jQuery 变量的值,该标签在核心标签库中可用。我如何使用该值?这是我的jsp:

<div data-role="page" data-theme="m" id="menu">
   <c:import url="/headerPage.jsp"></c:import>
    <div data-role="content">   
      <ul data-role="listview" data-theme="h" data-inset="true">
        <c:forEach items="${items}" var="item">
          <li>**<a href="#submenu1?id=${item.id}">**${item.name}</a></li>
        </c:forEach>
    </ul>
       </div>
  <c:import url="footerPage.jsp"></c:import>
</div>

    <div data-role="page" data-theme="m" id="submenu">
       <c:import url="/headerPage.jsp"></c:import>
        <div data-role="content">

           <c:set var="**testID**" scope="session" value="<div id='**result**'></div>"></c:set>  // here testID value printed 1st time but if I print value in 2nd time gives nothing

          <ul data-role="listview" data-theme="h" data-inset="true">

            <c:forEach items="${items}" var="item">

                      <c:if test="${menu.key eq **testID**}"> // here testID is not working
                     <li><a href="#submenu2">${item.name}</a></li>
                      </c:if>    
            </c:forEach>
        </ul>
           </div>
      <c:import url="footerPage.jsp"></c:import>
    </div>

    <script type="text/javascript">

       $(document).bind("pagebeforechange",function(e, data) {

        if (typeof data.toPage === "string") {
               var u = $.mobile.path.parseUrl(data.toPage), re2 = /^#submenu1/; 
               var categoryName = u.hash.replace( /.*id=/, "" ); // how to store categoryName in session and use it later in <c:if> tag in jsp.
               var pageSelector = u.hash.replace( /\?.*$/, "" );

               jQuery('#**result**').text(categoryName);

           $.mobile.changePage( $page, options );   
        }
    }
    </script>

【问题讨论】:

  • 您可以使用 jQuery 发出服务器请求并在服务器端存储需要存储的内容。

标签: javascript jquery jsp jquery-mobile jstl


【解决方案1】:

我不确定您需要做什么,但我看到了关键字 jQuery、变量、存储和会话。

如果您想要一种方法来存储页面范围内的变量,您可以使用 $.cookie()。

https://github.com/carhartl/jquery-cookie

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 2011-07-31
    • 2011-08-14
    • 2017-11-30
    • 2014-09-18
    • 1970-01-01
    • 2020-03-12
    相关资源
    最近更新 更多