2012-08-06 52 views
1

我已經JavaScript代碼如下:將JavaScript的數據轉換成表格

game.bind('gameover', function(seconds) { 
    setTimeout(function() { 
     var rank = game.getRank(seconds); 
     scorecard.find('.time').text(seconds + ' sec'); 
     scorecard.find('.rank').text(rank[0]); 
     scorecard.find('.byline').text(rank[1]); 
     ...more code 

在下面的div顯示此數據(所以JS腳本工作):

<div class="inner"> 
    <h3>Nice job! Your time:</h3> 
    <h1 class="wobble time"><!-- 15 seconds --></h1> 
    <h2 class="rank"><!-- Friendly Mallard --></h2> 
    <h3 class="byline"><!-- (That's pretty good) --></h3> 
</div> 

現在我想把結果也放在一個表格中,所以我可以把它發佈到數據庫(mysql)。所以我添加了一個表格,並把它放在同一div:

<form id="form" action="updatescore.php" method="post"> 
    <input type="text" id="time" name="time" value="" /> 
    <input type="text" id="rank" name="rank" value="" /> 
    <input type="text" id="byline" name="byline" value="" /> 
</form> 

現在的JS,我複製的線(例如scorecard.find('.time').text(seconds + ' sec');),並改變了它尋找的ID,所以.變得#和通過它在js像這樣:

scorecard.find('.time').text(seconds + ' sec'); 
scorecard.find('#time').text(seconds + ' sec'); 
etc 

所以這應該工作我認爲,但表單輸入保持空。我究竟做錯了什麼?

親切的問候,

莫里斯

回答

2

使用val()而不是text()設定值

+0

謝謝,我會嘗試這個,如果從Fresheyeball解決方案不起作用 – Maurice 2012-08-07 07:01:18

+0

這個工作。雖然另一種解決方案也可以,但我仍然需要一個表單,因爲我也有php數據來處理 – Maurice 2012-08-08 21:58:22

3

如果你只是希望通過updatescore.php到JS變量進入你的數據庫,你不需要使用一個表格!你可以用AJAX來做到這一點。

var scoreData = { 
     time : time, 
     rank : rank[0], 
     byline : rank[1] 
    }; 
    $.post('updatescore.php', scoreData); //done! 
+0

噢,我會試試這個 – Maurice 2012-08-07 07:00:37