當談到編碼時,我是一個極端的新手,但是無論如何它仍然存在。我有一個表格,其中有從MySQL數據庫填充的前兩列,每行獲取用戶輸入的文本框。然後寫回數據庫。這工作正常,我需要從每個文本框的用戶輸入中獲取總數,因爲他們正在輸入它,並從該頁面上較早的另一個文本框中減去該總數。我需要得到budgetmount輸入字段的總和。需要添加html輸入值,它是mysqli數組的一部分
這裏是我的表碼:
<span id="sum">0</span>
<?php
$result = mysqli_query($con,"SELECT envelopename, envelopebudget FROM envelopes");
echo "<table border='1' id='table_new_paycheck'>
<tr>
<th>Envelope</th>
<th>Budget</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename[]'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount[]" class="textboxone"></td><?php;
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?><br>
這裏是jQuery腳本我試過,但它不工作:
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
$(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".textboxone").each(function() {
$(this).keyup(function(){
calculateSum();
});
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
$(".textboxone").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").html(sum.toFixed(2));
}
我試圖去與jQuery認爲這會給我即時響應,因爲他們鍵入每個文本框的值,但我不知道如何去做這件事。這是我想要的一個例子,但我無法適應它與我的數組一起工作。 Here是我正在尋找的一個例子。它涵蓋了添加部分。
感謝您的幫助。
像這樣? http://jsfiddle.net/Newtt/Lseg900t/1/ – Newtt 2014-09-01 18:18:30
是的,但會有一個未定義數量的值,所以我不能給特定的變量添加。唯一的變量是他們輸入到未確定數目的文本框中的任何內容。 – Mike 2014-09-01 18:21:27
您可以重複剛纔的TR – Newtt 2014-09-01 18:21:49