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 );

这就是我现在所拥有的。

如果选中了两个复选框,我需要知道如何总计?

此功能将仅显示所选项目。

4

2 回答 2

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);

像这样?

于 2012-12-19T07:02:44.583 回答
0

我的 html 表单中有 2 个名为 vehicle 的复选框,如下所示, <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 );


 } 
于 2012-12-19T07:09:43.433 回答