2016-03-20 175 views
0

在我的JSP中,我根據JSTL的值調用JSP函數。我看到JS(內部JSTL)中的alert語句正在工作,但不是JQuery。這是爲什麼?爲什麼JSTL中的JS/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" /> 

請幫幫我。謝謝。

+0

如果「signUp_dialog」元素在DOM中的那個點之後出現*,那麼它將不起作用,因爲您的代碼不會找到該元素。 – Pointy

回答

0

您的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> 
0

你有沒有導入jQuery相關的文件? 另請檢查加載頁面後的查看源。

+0

這應該是一個評論而不是答案。 –

相關問題