【问题标题】:How to check a JSTL Variable in jQuery如何在 jQuery 中检查 JSTL 变量
【发布时间】:2014-12-08 20:09:20
【问题描述】:

在我的 JSP 中,我使用 JSTL 来设置某个变量:

<c:set var="myVar" value="true" scope="request" />

然后在 jQuery 中,在 $(document).ready(function() {..}) 中,我需要检查“myVar”的值。

<script type="text/javascript">
   $(document).ready(function() {
      if ("$(requestScope.myVar}") {
         // Case 1
      }
      else {
         // Case 2
      }
   });
</script>

这不起作用。 'if' 条件的计算结果始终为 TRUE

【问题讨论】:

    标签: javascript jquery jsp jstl struts


    【解决方案1】:

    您打错了,$(requestScope.myVar} 必须是 ${requestScope.myVar}(注意第一个括号)。

    另外,你应该使用

    ${requestScope.myVar eq true} 用于布尔比较

    ${requestScope.myVar eq 'true'} 用于字符串比较

    请看这里:set boolean value into variable using JSTL tags?

    【讨论】:

      【解决方案2】:

      你设置的变量是服务器端的,所以页面渲染后你的javascript看起来像

      if ("true") {
         // Case 1
      }
      else {
         // Case 2
      }
      

      要在页面呈现时检查服务器端的变量,您应该使用 JSTL

      <c:choose>
        <c:when test="${requestScope.myVar}">         
          // Case 1
        </c:when>
        <c:otherwise>
          // Case 2
        </c:otherwise>
      </c:choose>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-30
        • 2012-07-05
        • 2012-10-12
        • 1970-01-01
        • 2014-07-21
        • 1970-01-01
        • 2012-04-03
        • 1970-01-01
        相关资源
        最近更新 更多