2014-02-17 31 views
0

當我單擊按鈕時,警告消息不會顯示。如果我點擊一個按鈕,它應該顯示警報消息作爲延遲3分鐘的成功。當我單擊按鈕中的h:命令按鈕時,警告消息不會顯示

當我點擊按鈕時,首先它沒有響應第二次,只有它從bean獲取值。 爲什麼第一次不加載的原因是什麼?

XHTML代碼:

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html"> 
    <h:head> 
     <title>Facelet Title</title> 
     <script> 
      var myVar; 
      function myFunction() { 
       myVar = setTimeout(function(){message();},3000); 
      } 

      function message(){ 
       myVar=setTimeout(function() { 
        if(#{mytest.count}===0) { 
         alert("Fails"); 
        } 
        else { 
         alert("Success"); 
        } 
       },3000); 
      } 
     </script> 

    </h:head> 
    <h:body> 
     <h:form> 

      <h:commandButton onclick="message();" action="#{mytest.func}"  value="click"/> 

     </h:form> 
    </h:body> 
</html> 

Managed Bean的代碼:

package com.display; 

import javax.faces.bean.ManagedBean; 

@ManagedBean 
public class Mytest { 
    private int count=0; 

    public int getCount() { 
     return count; 
    } 

    public void setCount(int count) { 
     this.count = count; 
    } 

    public void func(){ 
     this.count=1; 
     System.out.println("function called"); 
     System.out.println("count value is -->"+count); 
    } 
} 
+0

從你在哪裏調用'myFunction的()'函數? –

+0

而不是我使用 user3048314

回答

0

夫婦的事情,我想指出。 (這可能不是答案,但我想提出一些可以幫助您解決問題的重構內容。)

。如果我點擊一個按鈕,它應該顯示警告消息成功,延遲3分鐘。

但按你的代碼是:

myVar = setTimeout(function(){message();},3000); //3 seconds (1000 ms = 1 seconds) 

。全局定義myvar,它被分配給功能myFunction()message。我的代碼中沒有看到myvar的使用。

編輯

基本上,你的JavaScript代碼得到纔去bean類調用。檢查你的commandbutton中是否有任何方法oncomplete

另外,您可以在包裝<f:ajax><h:commandButton>一樣,

<f:ajax onevent=myFunction();/> 
+0

我想我的代碼是myVar ..... – user3048314

+0

如果你不需要'myVar',最好刪除它。但我猜這不是問題。 –

+0

請檢查我的編輯。 –

相關問題