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

これは私が今持っているものです。

2 つのチェックボックスがオンになっている場合、合計する方法を知る必要がありますか?

この関数は、選択したアイテムのみを表示します。

4

2 に答える 2

0

次のように、html フォームに vehicle という名前の 2 つのチェック ボックスがあります。 <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 に答える
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 に答える