2012-10-12 65 views
1

我將創建一個計數器,該計數器在用戶按下數字鍵盤上的+按鈕時從0到100一步進行計數。使用+增加計數器並將值存儲到數據庫

這樣做的目的是創建一個商店櫃檯(我們在顯示器上顯示當前客戶編號,並且我希望它可以成爲基於Web的)。所以我使用的是jQuery,我希望當前的數字也存儲在MySQL數據庫中。

我該如何去做這件事?我不想使用表格,但如果我必須,我會。我嘗試了一些解決方案,但沒有一個是真正可用的。問題是我對jQuery沒有足夠的理解。所以向正確方向的幾個指針會很好。

我想流是這樣的:

我有一個包含一個數字,我會從數據庫中提取,到目前爲止好一個元素,我可以肯定這樣做我自己。然而,然後用戶在numpad上按下+,我希望該值增加1.然後,我希望腳本將這個新值存儲在數據庫中,並從數據庫中獲取新值並將其重新顯示在元素中。

正如我所說,我非常感謝與此有關的幾點。也許我正在推翻它。

我使用PHP的方式。

回答

1

所以,你需要處理被點擊的「+」按鈕,並進行AJAX調用。喜歡的東西:

http://jsfiddle.net/QE3xd/1/

HTML:

<input type="text" id="current" readonly="readonly" value="2" /> 
<input type="button" id="increaser" value="+" /> 

的Javascript:

$(document).ready(function() { 
    $("#increaser").on("click", add); 
}); 

function add() { 
    // Make AJAX call to PHP method (where DB operation is done) 

    $.ajax({ 
     url: "/echo/json/", 
     cache: false, 
     data: {"increment_by": 1}, // This line may be unnecessary, as you could assume it increases the value by 1 every time (but using this could make it customizable 
     dataType: "json", 
     success: function (data) { 
      // Should return data in format: {"new_current": 4} 
      //$("#current").val(data.new_current); 
      // ^^ the real line 
      $("#current").val(+$("#current").val()+1); 
      // ^^ just to simulate the new number being returned 
     }, 
     error: function() { 
      console.log("there was an error"); 
     } 
    }); 
} 
0

你所描述的是一個典型的MVVM問題。具有交互按鈕和顯示元素的用戶界面。

我對knockoutjs庫和jquery結合解決這類問題有一些很好的經驗。在教程中是example with a counter,就像你的一樣。

的2項其他要求(檢測數字鍵盤鍵和張貼的)可以與按鍵插件和jQuery $.post

可以容易解決
相關問題