2012-12-19 48 views
1
var bookRate = new Array('The Right to differ', 'Issues In Contemporary Documentary', 'Writing, Directing and Producing', 'Lee Kuan Yew My Lifelong Challenge'); 
var selection = document.rate.checkbox; 
var sum = 0.00 ; 

for (i=0; i<selection.length; i++) 

    if (selection[i].checked == true) 
    alert('Book Name : ' + bookRate[i] + ' \n Price of the book : ' + selection[i].value) 

alert('Total Purchased Books : ' + selection[i].value); 

這是我現在擁有的。如何獲得多個1複選框的值,以在JavaScript中總計它?

我需要知道如何總計,如果有兩個複選框選中?

此功能只顯示選定的項目。

+0

的HTML將真正幫助在這裏。 –

回答

0
var bookRate=['The Right to differ', 'Issues In Contemporary Documentary', 'Writing, Directing and Producing', 'Lee Kuan Yew My Lifelong Challenge']; 
var selection=document.rate.checkbox; 
var books=0,sum=0.00; 

for(var i=0;i<selection.length;i++) 
{ 
    if(selection[i].checked) 
    { 
     books++; 
     sum+=(parseFloat(selection[i].value,10) || 0); 
    } 
} 
alert("Total purchased book: "+books+"\nTotal price: "+sum); 

是這樣的?

+0

它不工作? :( – user1914971

+0

@ user1914971我在原答案中輸入了一個錯字,現在怎麼辦?請告訴我們,如果它「不工作」,你會得到什麼結果 – Passerby

+0

總價格不正確,總價的結果是0100.9099.9 – user1914971

0

我有2個在我的HTML表單命名車輛,這樣的複選框, <form action=""> <input type="checkbox" name="vehicle" value="5" onclick="check()">vehicle 1 price<br> <input type="checkbox" name="vehicle" value="4" onclick="check()">vehicle 2 price </form>

和我的JavaScript函數

function check() 
    { 
    var bookRate = new Array('The Right to differ', 'Issues In Contemporary Documentary', 'Writing, Directing and Producing', 'Lee Kuan Yew My Lifelong Challenge'); 
    var selection = document.getElementsByName('vehicle'); 
    var sum = 0.00 ; 

for (i=0; i<selection.length; i++) 
    { 
    if (selection[i].checked == true) 
    { 
    sum=sum + parseInt(selection[i].value); 

} 
} 
    alert('Total sum of Purchased Books : ' + sum); 


} 
+0

這是用於JavaScript的。 – user1914971

+0

,因爲它沒有工作? – user1914971

+0

沒有它的jqeury解決方案,讓我更新代碼爲javascript –

相關問題