【问题标题】:Why is JS/JQuery function inside JSTL is not working?为什么 JSTL 中的 JS/JQuery 函数不起作用?
【发布时间】:2016-03-20 13:47:54
【问题描述】:

在我的 JSP 中,我根据 JSTL 的值调用了一个 JSP 函数。我看到 JS(在 JSTL 内部)中的警报语句正在工作,但不是 JQuery。这是为什么呢?

<c:set var = "error" value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>
<c:choose>                  

    <c:when test="${error=='not unregistered'}"> 
            <script type="text/javascript">
               alert("Hi"); 
               openSignUp();
            </script>                                                                               
    </c:when>
    <c:otherwise>
        <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />                                             
    </c:otherwise>                                      
</c:choose>                     
<c:remove var = "SPRING_SECURITY_LAST_EXCEPTION" scope = "session" />   

<script>
   function openSignUp(){
         $('#signUp_dialog').dialog({
            width:350,
            open: function(event, ui) {
            $(".ui-button-text").remove();
            $(".ui-dialog-title").css("margin-right","275px");
            }
         });                    
    }

</script>

我也尝试了以下方法,将代码直接放在 JS 函数中的 JSTL 脚本标记中。还是没有运气。

<c:set var = "error" value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>
<c:choose>                  
    <c:when test="${error=='not unregistered'}"> 
        <script type="text/javascript"  src="<c:url value="/resources/scripts/jquery-ui-1.11.4/external/jquery/jquery.js" />">
        $(document).ready(function() {  
            $('#signUp_dialog').dialog({

                width:350,
                open: function(event, ui) {
                    alert("hi");
                    $(".ui-button-text").remove();
                $(".ui-dialog-title").css("margin-right","275px");
                }
             });    
        });     
        </script>                                                                               
    </c:when>
    <c:otherwise>
        <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />                                             
    </c:otherwise>                                      
</c:choose>                     
<c:remove var = "SPRING_SECURITY_LAST_EXCEPTION" scope = "session" />   

请帮助我。谢谢。

【问题讨论】:

  • 如果 "signUp_dialog" 元素出现在 DOM 中 之后 的那个点,那么它就不起作用,因为您的代码找不到该元素。

标签: javascript jquery jsp spring-mvc jstl


【解决方案1】:

您的 javascript 代码(无论是纯 java 脚本还是 jquery)应该在页面加载时触发(在所有 java/jsp/jstl 处理完成之后)。因此,请在 JSP 页面末尾的脚本块中调用 openSignUp(在处理完所有元素之后)。

...JSP page
<script>
//openSignUp definition here

//check the condition first
if(${error} == 'not unregistered'})//this should translate to either true or false
{
  openSignUp();//if the conditon is true, call the dialog
}
</script>

【讨论】:

    【解决方案2】:

    你导入jquery相关文件了吗? 加载页面后还要检查查看源代码。

    【讨论】:

    • 这应该是评论而不是回答。
    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 2022-11-12
    • 1970-01-01
    • 2014-05-29
    • 2015-12-27
    • 2016-06-28
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多