2013-06-19 31 views
0

我不知道我做錯了什麼,但我只是想在你做出選擇後得到總價。我想獲取選擇的值並將其乘以100,然後使用updatetotal函數來顯示總數。從簡單的Javascript加法乘法需要值

// Collect Data & Prices To Update Dynamic Prices 
var pricechecking=document.getElementById('howmanyaccts')value.trim(); 
var pricepaypal=document.getElementById('howmanyacctspaypal')value.trim(); 

var pricecheck = pricechecking * 100; 
var pricepay = pricepaypal * 100; 

function updateTotal() { 

var ThePrice = pricecheck + pricepay; 

$('#TotalPrice').text('$' + ThePrice + '.00'); 
} 

$(function() { $('.DoPricing').click(updateTotal); }); 

我的HTML是:

<form action="http://linktomyactionpage" method="post" > 
<p>How many accounts to pay by check?</p> 
<select name="howmanyaccts" id="howmanyaccts" class="DoPricing"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
    <option value="5">5</option> 
</select> 

<p>How many accounts to pay by paypal?</p> 
<select name="howmanyacctspaypal" id="howmanyacctspaypal" class="DoPricing"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
    <option value="5">5</option> 
</select> 

<p>Total: <span id="TotalPrice">$0.00</span></p> 

<input type="submit" name="submit" value="submit"> 
</form> 

如果我簡化了代碼? 00.00的文本和沒有乘法?

// Collect Data & Prices To Update Dynamic Prices 
var pricechecking=document.getElementById('howmanyaccts').value.trim(); 
var pricepaypal=document.getElementById('howmanyacctspaypal').value.trim(); 

function updateTotal() { 

    var ThePrice = pricechecking + pricepaypal; 

    $('#TotalPrice').text('$' + ThePrice + '00.00'); 
} 

$(function() { $('.DoPricing').click(updateTotal); }); 

這裏是我開始工作:

// Collect Data & Prices To Update Dynamic Prices 
var pricechecking=document.getElementById('howmanyaccts').value.trim(); 
var pricepaypal=document.getElementById('howmanyacctspaypal').value.trim(); 

function updateTotal() { 
var pricecheck = parseInt($('#howmanyaccts').val(), 10); 
var pricepay = parseInt($('#howmanyacctspaypal').val(), 10); 

    var ThePrice = pricecheck + pricepay; 

    $('#TotalPrice').text('$' + ThePrice + '00.00'); 
} 

$(function() { $('.DoPricing').click(updateTotal); }); 
+0

錯誤'.getElementById('howmanyacctspaypal ')value' – elclanrs

+0

對於初學者,你應該在'getElementById(...)。value'的兩個地方寫'getElementById(...)value'。如果您需要更詳細的幫助,請考慮更詳細地說明您面臨的問題。 –

+0

@David - 我沒有得到總計 –

回答

1

我想你想要做這樣的事情:http://jsfiddle.net/F3HbJ/

$(function() { 
    $('.DoPricing').change(function() { 
     var total = 0; 
     $('.DoPricing').each(function() { 
      total += parseInt($(this).val()) * 100; 
     }); 
     $('#TotalPrice').html('$' + total); 
    }); 
}); 

事件處理程序是.change()的選擇。然後你遍歷每個parseInt值選擇您乘以100

+0

是的,我只是想出來了!謝謝! –

+0

你的代碼效果更好!我的代碼在我的網站上無法使用。再次感謝! –

0

這裏工作DEMO

使用這樣的:這裏

$(document).ready(function() { 
     var pricechecking = $($("#howmanyaccts")[0].selectedOptions).attr("value"); 
     var pricepaypal = $($("#howmanyacctspaypal")[0].selectedOptions).attr("value"); 

     function updateTotal() { 
      var pricecheck = parseInt($('#howmanyaccts').val(), 10); 
      var pricepay = parseInt($('#howmanyacctspaypal').val(), 10); 

      var ThePrice = pricecheck + pricepay; 

      $('#TotalPrice').text('$' + ThePrice + '00.00'); 
     } 

     $('.DoPricing').change(updateTotal); 
    });