2010-11-30 158 views
0

我有一個AJAX /原型相關的問題。我有一個AJAX調用當前擺內聯,在onclick(我保證它搬出那裏:)事件,看起來像這樣:AJAX /原型重置呼叫

onclick="var pn = $('yards').value; $('inventory').update('Working...'); 
new Ajax.Updater('inventory','ajax.php?ac=checkInventory&productID='.$someproductid.'&yards='+pn,{method:'get', evalScripts: true, asynchronous: true, onComplete:function(tr){$('inventory').update(tr.responseText);}});" 

文件名爲ajax.php然後從$ _GET獲取數據[],顯示一個小文字輸入欄。當填寫並點擊提交按鈕時,它會調用一個從文件讀取相關數據的功能,並在屏幕上打印結果。所有這些代碼的目的都是庫存檢查。

因此,當用戶點擊上面定義的onclick的「獲取庫存」按鈕時,一切正常,用戶填滿碼數,彈出正確的結果,每個人都高興。但是,它的可用性較差的事實是,如果用戶想要執行另一個庫存檢查,則需要刷新整個頁面。如果不是,那麼當點擊「獲取庫存」按鈕時,用戶將再次獲得與上次檢查相同的結果,而不是文本輸入字段。

所以,我的問題是,如何在一次庫存檢查後做出這樣的事情,所有事情都會重置,以便下一次用戶點擊「獲取庫存」按鈕時,他將被提供填補文本輸入字段,從而得到新的結果。

如果我沒有讓自己清楚,我很抱歉。我對AJAX和原型非常陌生,這是我需要完成的同事的工作......謝謝!

回答

0

啊我看,

我不認爲Ajax.Updater是這樣工作的;看看在文檔: http://www.prototypejs.org/api/ajax/updater

new Ajax.Updater({ success: 'items', failure: 'notice' }, '/items', { 
    parameters: { text: $F('text') }, 
    insertion: Insertion.Bottom 
}); 

var pn = $('yards').value; 
    $('inventory').update('Working...'); 
    new Ajax.Updater('inventory','/ajax.php 
,{ 
    parameters: { ac: 'checkInventory' 
        , productId: /*put productId here*/ 
        , yards:/*put yards here too*/ 
       } 
, method:'get' 
, evalScripts: true 
, asynchronous: true 
, onSuccess: function(tr){ 
      //try an alert(tr.repsonseText); just to see what you get back 
      $('inventory').update(tr.responseText); 
      } 
}); 

事情嘗試:你重新填充之前

  • 清除文本輸入字段,當它在你的Ajax.Updater調用的onComplete事件觸發。

  • 警告響應以確保您從服務器獲得不同的響應。

    *使用Fiddler,以確保您傳遞一個不同的productId

  • 變化的onComplete到的onSuccess

希望這有助於

+0

謝謝,它確實有助於d ebugging過程。最後,我把它放在了onclick之外,並且設置了整個函數,以便它的onclick被調用並顯示文本輸入字段。這樣每次用戶點擊該按鈕時都會重新啓動。 – Spaceploit 2010-12-14 04:53:41

0

既然你已經答應移動碼出onclick處理程序如下:

var inv = $('inventory'); 

inv.update('Working...'); 

new Ajax.Request('ajax.php', { 
    method:'get', 
    parameters:{ 
     ac:'checkInventory', 
     yards:$('yards').value 
    }, 
    onSuccess: function(response) 
     { 
      inv.update(response.responseText); 
     } 
}); 
+0

也謝謝你;) – Spaceploit 2010-12-14 04:54:04