2012-12-19 3 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); 

내가 지금 가지고있는 것.javascript에서 두 개 이상의 확인란 값을 합산하는 방법은 무엇입니까?

두 개의 체크 박스가 선택되어 있으면 합계하는 법을 알아야합니까?

이 기능은 선택한 항목 만 표시합니다.

+0

입니다. –

답변

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

나는이 같은 내 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>

이 내 자바 스크립트 함수는 HTML 정말 여기에 도움이 될

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

자바 스크립트 용입니다. – user1914971

+0

가 작동하지 않았기 때문에? – user1914971

+0

아니 jqeury 솔루션, 자바 스크립트 –

관련 문제