2012-11-02 47 views
0

我的問題關於setTimeout在我的HTML代碼我再次使用ajax與jquery和我的代碼。並且我使用primefaces jsf。爲什麼我使用jQuery的settimeout與ajax keyup事件你能告訴我爲什麼jQuery的settimeout不工作與AJAX鍵盤事件

這是我的jQuery代碼和它現在工作,但是當我添加settimeout它不工作。

$ = jQuery; 

$(document).ready(function() 
{ 
    setTimeout(function() 
    { 
     refreshHook(); 
    },1000) 

}); 


function refreshHook() 
{ 
    $(".numberonly").keydown(function(event) 
    { 
     // Allow: backspace, delete, tab and escape 
     if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode==190 || event.keyCode == 9 || event.keyCode == 27 || 

     // Allow: Ctrl+A 
     (event.keyCode == 65 && event.ctrlKey === true) || 

     // Allow: home, end, left, right 
     (event.keyCode >= 35 && event.keyCode <= 39)) { 

     // let it happen, don't do anything 
     return; 
    } 
    else 
    { 
     // Ensure that it is a number and stop the keypress 
     if ( event.shiftKey|| (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) 
     { 
      event.preventDefault(); 
     } 
    } 

    }); 
    } 

這是我的HTML代碼。

<p:fieldset legend="Fieldset" style="background-color: lightsteelblue" > 
    <h:panelGrid columns="5"> 
    <strong>LandAmoutn(Ha):</strong> 
    <p:inputText id="spinner1" type="text" styleClass="numberonly" value="#{carbonController.model.meyvebahcesidikimi}" style="background-color: lightgrey" > 
    <p:ajax event="keyup" process="@this" update="birdikim" listener="#{carbonController.doMeyveBahcesiDikimHesabı}"/> 

    </p:inputText> 
    <p:row><strong>=</strong></p:row> 
    <p:inputText id="birdikim" value="#{carbonController.model.meyvebahcesidikimsonuc}" readonly="true" style="background-color:lightgrey"> 
    <f:convertNumber integerOnly="true"/> 
    </p:inputText> 
    <p:row><strong>Mg C/Ha</strong></p:row> 
    </h:panelGrid> 
</p:fieldset> 

有人可以告訴我我的錯誤在哪裏。我花了很多times.I的想送我的圖片,但StackOverflow的doenst允許發送:)

+2

你的失蹤;後},1000) – Vucko

+0

另外,你是否引用jQuery庫? –

+2

你想做什麼?誠實地說'setTimeout'沒有用。如果你想爲未來的元素附加'keydown'事件,你應該看看['.on()'](http://api.jquery.com/on/)。 (請說明你的問題)(http://sscce.org/) – Alexander

回答

2

試試這個

$(document).ready(function() { 
    setTimeout(refreshHook, 1000); 
}); 

這似乎是工作http://jsfiddle.net/cQjmx/5/

編輯 - 不知道如果這是你以後的事情,但因爲沒有人發佈任何東西給這個去http://jsfiddle.net/cQjmx/37/

+0

我想在3秒後看到id =「birdikim」的結果。我也在我的項目中使用ajax。當用戶輸入號碼自動更新結果,但我想在3秒後看到結果。希望我清楚。 thx爲興趣 –

+0

以及您的所有代碼和我的帖子中的固定代碼將要做的是在$(document).ready()的第一秒中允許對該文本框進行任何輸入。 –

+0

對不起,我不明白。皮特。問題是setTimeout –

相關問題