因此,我試圖創建一個小表,用於計算每隻鴨子有一定賠率時在橡皮鴨競賽上下注的潛在收益。jQuery計算一次更新一行
我有點有這個工作,但都創下了絆腳石......
頁面加載所有的數學是基於£10的默認值正確。然後我想要做的是允許人們改變他們想要每個鴨子下注的金額,並且該鴨子的潛在獎金只會自動更新。
這是我到目前爲止有:
function calculate_odds(row) {
var winnings = 0;
// Set winnings to 0
var odds = $(row).find('#theodds').attr('data-duckodds'),
// Find the value of the data attribute for a specific td
betting_amount = $('[name="betting-amount"]').val(),
// Find the value entered into an input field
winnings = (parseFloat(odds)/1 + 1) * betting_amount;
// Work out the winnings based on the odds given.
// Divide first number by 1 as all odds are something to 1 for now, then +1
// Multiply that number by the bet
// For example Bet 30 on 3/1 odds
// 3/1 = 3 + 1 = 4 * 30 = 120
$(row).find('.js-winnings').html(winnings.toFixed(2));
// Show the winnings amount in the final td in each row
}
$(document).ready(function() {
$('.lineup tbody tr').each(function() {
// Run the function calculate_odds() initially based on the default value;
calculate_odds();
$(this).find('[name="betting-amount"]').on('keyup', function() {
// Now loop through each row and change the winnings amount if the betting amount is changed
calculate_odds($(this).closest('tr'));
}).trigger('keyup');
})
});
從我的理解(我的jQuery是不是很大),這條線內:$(this).find('[name="betting-amount"]').on('keyup', function() {
所有我需要做的是選擇特定行我想更新。這應該是簡單的權利?
取而代之的是從第一行獲取更新後的值,然後在更改後面的行時應用。
任何人都可以指出我們要出錯的地方嗎?你可以在這裏看到計算器:http://www.kb-client-area.co.uk/ducks/races/race-1/
感謝提前:)
您的HTML無效,因爲您有重複的ID。首先修復它,否則會造成麻煩。 – lshettyl